spring bean circular references

Seen a spring open class, record what the bean circular reference problem.

problem:

public class IndexService{

  @Autowired

  IndexDao indexDao;

}

public class IndexDao{

  @Autowired

  Index Service index service;

}

The above example IndexService dependent IndexDao, IndexDao dependent IndexService.

bean spring in the instantiation process:

  Go to create IndexDao bean,

1. Create IndexDao instance, generated at this time has not IndexDao bean.

2. IndexDao to configure properties of an object, this property is IndexService, in this configuration process will put its own (IndexDao) objects into singleFactory in;

3. Then go to find IndexService bean filling, found a single case of pool (specialized storage bean) does not, then go to singleFactory in, or not.

4. Create IndexService instance, at this time no IndexService bean.

5. IndexService to configure object property that is IndexDao, in this configuration process will put its own (IndexDao) the objects into singleFactory;

6. Then to find IndexDao bean to fill, not found in a single case of the pool, and then go to the singleFactory found there.

7. IndexService then filled into the property.

8. After 4,5,6,7 have IndexService, and at this time has a value of the attribute IndexDao, then in step 3 then injected into the IndexService IndexDao

9. Then continue with the subsequent initialization IndexDao bean.

Guess you like

Origin www.cnblogs.com/yongan/p/11354705.html