The InitializingBean interface provides a way to initialize methods for beans. It only includes the afterPropertiesSet method. Any class that inherits the interface will execute the method when initializing the bean.
Tested as follows:
import org.springframework.beans.factory.InitializingBean;public class TestInitializingBean implements InitializingBean{ @Override public void afterPropertiesSet() throws Exception { System.out.println("ceshi InitializingBean"); } public void testInit(){ System.out.println("ceshi init-method"); }}Configuration File
<bean id="testInitializingBean" ></bean>
The Main function is as follows
public class Main { public static void main(String[] args){ ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/java/com/beans.xml"); }}The test results are:
ceshi InitializingBean
This means that when spring initializes beans, if the bean implements the InitializingBean interface, the afterPropertiesSet method will be automatically called.
So the problem is that when configuring beans, you can also configure the initialization method for the bean using the init-method configuration. Which of these two will be executed first? Next, test it, modify the configuration file, and add init-method:
<bean id="testInitializingBean" init-method="testInit"></bean>
Run the program and get the result:
ceshi InitializingBean
ceshi init-method
From the results, we can see that when Spring initializes the bean, if the bean implements the InitializingBean interface and specifies the init-method in the configuration file, the system first calls the afterPropertieSet() method, and then calls the method specified in the init-method.
So how is this method implemented in spring? By looking at the source code class of Spring loading beans, you can see the mystery. The invokeInitMethods in the AbstractAutowiredCapableBeanFactory class is very clear, as follows:
protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable { //Judge whether the bean has implemented the InitializingBean interface. If the InitializingBean interface is implemented, only the afterPropertiesSet method of the bean is called 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<Object>() { public Object run() throws Exception { //Directly call afterPropertiesSet ((InitializingBean) bean).afterPropertiesSet(); return null; } },getAccessControlContext()); } catch (PrivilegedActionException pae) { throw pae.getException(); } } else { //Directly call afterPropertiesSet ((InitializingBean) bean).afterPropertiesSet(); } } } if (mbd != null) { String initMethodName = mbd.getInitMethodName(); //Distinguish whether the init-method method is specified. If the init-method method is specified, then the established init-method if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) { //Further look at the source code of this method, you can find that the method specified in the init-method method is to implement invokeCustomInitMethod(beanName, bean, mbd); } }}Summarize:
1. Spring provides beans with two ways to initialize beans, implement the InitializingBean interface, implement the afterPropertiesSet method, or specify it through init-method in the configuration file. Both methods can be used at the same time.
2. Implementing the InitializingBean interface directly calls the afterPropertiesSet method, which is a little more efficient than calling the method specified by init-method through reflection, but the init-method method eliminates the dependence on spring.
3. If an error occurs when calling the afterPropertiesSet method, the method specified by init-method is not called.
The role of Spring InitializingBean
Spring's InitializingBean interface is very useful. It is located in spring beans. It only provides one method afterPropertiesSet(). When you implement this method, spring will provide you with framework-level support: when you produce an instance of the class that implements the interface through the sring container, it will call the afterPropertiesSet method. Through this method, you can check whether your bean is initialized correctly. Of course, you can also use the init-method method. These two methods can be used at the same time, and the order of calls is init-method.
Summarize
The above is the use of the InitializingBean interface in Spring introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!