In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to apply Spring core interface InitializingBean". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to apply Spring core interface InitializingBean".
1. InitializingBean interface description
The InitializingBean interface provides bean with the processing method after property initialization, which only includes the afterPropertiesSet method. All classes that inherit the interface will execute this method after the property initialization of bean.
Package org.springframework.beans.factory
/ * *
* Interface to be implemented by beans that need to react once all their
* properties have been set by a BeanFactory: for example, to perform custom
* initialization, or merely to check that all mandatory properties have been set.
*
*
An alternative to implementing InitializingBean is specifying a custom
* init-method, for example in an XML bean definition.
* For a list of all bean lifecycle methods, see the BeanFactory javadocs.
*
* @ author Rod Johnson
* @ see BeanNameAware
* @ see BeanFactoryAware
* @ see BeanFactory
* @ see org.springframework.beans.factory.support.RootBeanDefinition#getInitMethodName
* @ see org.springframework.context.ApplicationContextAware
, /
Public interface InitializingBean {
/ * *
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware)
*
This method allows the bean instance to perform initialization only
* possible when all bean properties have been set and to throw an
* exception in the event of misconfiguration.
* @ throws Exception in the event of misconfiguration (such
* as failure to set an essential property) or if initialization fails.
, /
Void afterPropertiesSet () throws Exception
}
It is also clear from the method name afterPropertiesSet that the method is called after the property has been set.
Second, the application of source code analysis interface
By looking at spring's source code class (AbstractAutowireCapableBeanFactory) that loads bean, you can see
Protected void invokeInitMethods (String beanName, final Object bean, RootBeanDefinition mbd)
Throws Throwable {
/ / determine whether the bean implements the InitializingBean interface, and if the InitializingBean interface is implemented, call the afterPropertiesSet method of bean
Boolean isInitializingBean = (bean instanceof InitializingBean)
If (isInitializingBean & & (mbd = = null | |! mbd.isExternallyManagedInitMethod ("afterPropertiesSet") {
If (logger.isDebugEnabled ()) {
Logger.debug ("Invoking afterPropertiesSet () on bean with name'" + beanName + "'")
}
If (System.getSecurityManager ()! = null) {
Try {
AccessController.doPrivileged (new PrivilegedExceptionAction () {
Public Object run () throws Exception {
/ / call afterPropertiesSet
(InitializingBean) bean) .afterPropertiesSet ()
Return null
}
}, getAccessControlContext ()
}
Catch (PrivilegedActionException pae) {
Throw pae.getException ()
}
}
Else {
/ / call afterPropertiesSet
(InitializingBean) bean) .afterPropertiesSet ()
}
}
If (mbd! = null) {/ / determines whether the init-method method is specified, and if the init-method method is specified, then call the established init-method
String initMethodName = mbd.getInitMethodName ()
If (initMethodName! = null & & (isInitializingBean & & "afterPropertiesSet" .equals (initMethodName)) & &
! mbd.isExternallyManagedInitMethod (initMethodName)) {
/ / reflection calls the init-method method
InvokeCustomInitMethod (beanName, bean, mbd)
}
}
}
By analyzing the code, you can understand:
1:spring provides bean with two ways to initialize bean, to implement the InitializingBean interface, to implement the afterPropertiesSet method, or to specify the same as init-method in the configuration file, both of which can be used at the same time
2: implementing the InitializingBean interface is to call the afterPropertiesSet method directly, which is relatively more efficient than calling the method specified by init-method through reflection. But the init-method approach eliminates the dependence on spring
3: if an error occurs when calling the afterPropertiesSet method, the method specified by init-method is not called.
III. Interface application
The InitializingBean interface itself has many applications in the spring framework, which is needless to say. How do we use this interface in practical applications?
1. Use the InitializingBean API to process a configuration file:
Import java.io.File
Import java.io.FileInputStream
Import java.util.Properties
Import org.springframework.beans.factory.InitializingBean
Public class ConfigBean implements InitializingBean {
/ / Wechat official account profile
Private String configFile
Private String appid
Private String appsecret
Public String getConfigFile () {
Return configFile
}
Public void setConfigFile (String configFile) {
This.configFile = configFile
}
Public void afterPropertiesSet () throws Exception {
If (configFileholders null) {
File cf = new File (configFile)
If (cf.exists ()) {
Properties pro = new Properties ()
Pro.load (new FileInputStream (cf))
Appid = pro.getProperty ("wechat.appid")
Appsecret = pro.getProperty ("wechat.appsecret")
}
}
System.out.println (appid)
System.out.println (appsecret)
}
}
2. Configuration
Spring profile:
Wechat.properties profile
Wechat.appid=wxappid
Wechat.appsecret=wxappsecret
3. Testing
Public static void main (String [] args) throws Exception {
String config = Test.class.getPackage (). GetName (). Replace ('.','/') + "/ bean.xml"
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext (config)
Context.start ()
}
Thank you for reading, the above is the content of "how to apply Spring core interface InitializingBean". After the study of this article, I believe you have a deeper understanding of how to apply Spring core interface InitializingBean, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.