This article mainly studies the relevant contents of the Spring bean loading execution order, as follows.
Source of the problem:
There is a bean that is A and a bean that is B. When A is instantiated, you want to assign a property name to a method funB to the return value of B.
If it is just written in A:
private B b;
private String name = b.funb();
An error will be reported and nullpointException is because b has not been set in at this time, so it is null.
The solution is as follows code, and at the same time, learn the execution order of InitializingBean, object construction method, and init-method in spring.
public class A implements InitializingBean {private B b;private String name;// = b.funb(); public void setB(B b) {System.out.println("A.setB initialized"); this.b = b;}public A() {System.out.println("A initialed");}public void init() {System.out.println("init"); this.name = b.funb();}@Override public String toString() {return super.toString() + this.name;}public void afterPropertiesSet() throws Exception {//In fact, it is also possible to put it here//this.name = b.funb(); System.out.println("afterPropertiesSet");}}public class B {public String funb() {System.out.println("funb");return "B.funb";}public B() {System.out.println("B initialed");}}spring configuration file
<beans default-autowire="byName"> <bean id="a" init-method="init"> </bean> <bean id="b"> </bean> </beans>
Test code:
public static void main(String[] args) {ApplicationContext context = new FileSystemXmlApplicationContext( "src/testspring/bean.xml");A a = (A) context.getBean("a");System.out.println(a);}The program output is:
A initialed
B initialed
A.setB initialed
AfterPropertiesSet
init
funb
[email protected]
From here, we can see that A's name attribute is successfully set to the return value of the funB method of B when the bean is loaded. The key point is to use init-method to implement it.
The loading order can also be seen as:
First constructor -> then b set method injection -> InitializingBean afterPropertiesSet method -> init-method method
The following content is excerpted from the book, but I found that even if I excerpt it once, my understanding of its content will be more in-depth!
1. The process of Spring assembly bean
1. Instantiation;
2. Set attribute value;
3. If the BeanNameAware interface is implemented, call setBeanName to set the Bean ID or Name;
4. If you implement the BeanFactoryAware interface, call setBeanFactory to set BeanFactory;
5. If ApplicationContextAware is implemented, call setApplicationContext to set ApplicationContext
6. Call the pre-initialization method of BeanPostProcessor;
7. Call the afterPropertiesSet() method of InitializingBean;
8. Call the customized init-method method;
9. Call the post-initialization method of BeanPostProcessor;
Spring container shutdown process
1. Call DisposableBean destroy();
2. Call the customized destroy-method method;
1. Single Bean
load
1. Instantiation;
2. Set attribute value;
3. If the BeanNameAware interface is implemented, call setBeanName to set the Bean ID or Name;
4. If you implement the BeanFactoryAware interface, call setBeanFactory to set BeanFactory;
5. If ApplicationContextAware is implemented, call setApplicationContext to set ApplicationContext
6. Call the pre-initialization method of BeanPostProcessor;
7. Call the afterPropertiesSet() method of InitializingBean;
8. Call the customized init-method method;
9. Call the post-initialization method of BeanPostProcessor;
Spring container close
1. Call DisposableBean destroy();
2. Call the customized destroy-method method;
2. The order of multiple beans
Prioritize loading of BeanPostProcessor implementation Bean
In order of bean definition, the order of loading of beans (even if there is id overwrite when loading multiple spring files)
When "Set attribute value" (step 2), when encountering ref, the bean corresponding to the id of ref is loaded first after "Instantiation" (step 1).
The subclass of AbstractFactoryBean will call the createInstance method after step 6, and then the getObjectType method will be called.
The BeanFactoryUtils class will also change the loading order of the beans.
The above is all the content of this article about the implementation order of Spring bean loading, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!