spring circular reference problem, a problem was found in the process of commissioning a small partner even put up a circular reference is set to false. Baidu's estimates are not careful when additional bar code. . .

Circular reference attribute operations:

1) AbstractAutowireCapableBeanFactory class allowCircularReferences is set to false.

2) Code:

AnnotationConfigApplicationContext a = new AnnotationConfigApplicationContext("");

a.setAllowCircularReferences();

spring initialization process

1 spring initialization context, new DefaultListBeanFactory ()

2 After calling refresh ()

3 refresh () contains all of the initialization process springBean

4 complete scans all spring Bean, in invokeBeanFactoryPostProcessors ()

5 in which the core of the initialization method is finishBeanFactoryInitialization (incoming beanFactory)

Continue to call after 6 preInstantiateSingletons, ready to enter the initialization.

7 core methods continue to call getBean, because getBean will really initialization Bean. Then getBean will be called twice getSingleton method, "the first call is to solve the problem of mutual references Bean, passed as the second parameter default true".

8 first call getSingleton basic returns null because this time the bean is not created, so singleobjectsMap also no way to find create success Bean, this time to continue will determine whether the current Bean is in the created state, and if so to obtain from earlySingletonObjectsMap in You must get to; if you do not create a state, then this time the first call returns null.
*** In the case described here, bean is just at the beginning "created" status, which is a separate object is created, but dependency injection have not done, so the objects placed in earlySingletonObjectsMap!


9 secondary call getSingleton method, initialize the bean also checks singleobjectsMap collection time, if you still can not get it to perform createBean method to create objects, indirect objects called doCreateBean create wapper, wapper target is equivalent to the class object itself to provide "post processor ", then start the instance of the object after the object has been created" injection. "
ps: spring BeanFactory that a total of eight post-processor.

01 injection method is by populateBean method to begin encountered @Autowired property is to find the appropriate BeanDefine call getBean method performs 6 steps to again by the post-processor BeanFactory.
ps: two previously mentioned methods to deal with the problem getSingleton Bean refer to each other's answers will gradually emerge; below:

 

Guess you like

Origin www.cnblogs.com/zzq-include/p/11979048.html