Spring bean load execution sequence instance parsing, springbean
This article focuses on the loading and execution sequence of Spring bean. The details are as follows.
Source:
One bean is A and one bean is B. If you want A to assign A property name to the return value of funB in the method of B during container instantiation.
If it simply says:
private B b;
private String name = b.funb();
It will report an error saying nullpointException, because at this time B has not been set in, so it is null.
The solution is as follows: Learn the execution sequence of InitializingBean, object constructor, 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 (". setB initialed "); 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 {// It can also be stored 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" class="testspring.A" init-method="init"> </bean> <bean id="b" class="testspring.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);}
Program output:
A initialed
B initialed
A. setB initialed
AfterPropertiesSet
Init
Funb
Testspring.A@50d89cB.funb
Here we can see that the name attribute of A is also set as the return value of the funB method of B when bean loading is complete. The key point is to use init-method.
The loading order can also be seen:
First constructor --> then set method injection of B --> afterPropertiesSet method of InitializingBean --> init-method
Summary:
The following content is excerpted from the book, but I find that even if you extract it again, your understanding of the content will be further enhanced!
I. Bean assembly process in Spring
1. instantiation;
2. Set the property value;
3. If the BeanNameAware interface is implemented, call setBeanName to set the Bean ID or Name;
4. If the BeanFactoryAware interface is implemented, call setBeanFactory to set BeanFactory;
5. If ApplicationContextAware is implemented, call setApplicationContext to set ApplicationContext
6. Call BeanPostProcessor's pre-initialization method;
7. Call the afterPropertiesSet () method of InitializingBean;
8. Call the custom init-method;
9. Call BeanPostProcessor's post-initialization method;
Spring container disabling process
1. Call destroy () of DisposableBean ();
2. Call the customized destroy-method;
I. Single Bean
Load
1. instantiation;
2. Set the property value;
3. If the BeanNameAware interface is implemented, call setBeanName to set the Bean ID or Name;
4. If the BeanFactoryAware interface is implemented, call setBeanFactory to set BeanFactory;
5. If ApplicationContextAware is implemented, call setApplicationContext to set ApplicationContext
6. Call BeanPostProcessor's pre-initialization method;
7. Call the afterPropertiesSet () method of InitializingBean;
8. Call the custom init-method;
9. Call BeanPostProcessor's post-initialization method;
Disable spring container
1. Call destroy () of DisposableBean ();
2. Call the customized destroy-method;
Ii. Sequence of multiple beans
BeanPostProcessor implementation Bean loading first
Sort by Bean file and Bean definition order by bean Loading Order (even if multiple spring files are loaded with id override)
When a ref is encountered during "set attribute values" (step 1), the bean corresponding to the ref id is loaded after "instantiation" (step 2 ).
Subclass of AbstractFactoryBean. After step 2, The createInstance method is called, and the getObjectType method is called later.
The BeanFactoryUtils class also changes the Bean loading sequence.
Summary
The above is all the content about Spring bean loading and execution sequence instance parsing. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!