Circular dependency in Spring

How does Spring resolve this: bean A is dependent on bean B, and bean B on bean A.


  • As the other answers have said, Spring just takes care of it, creating the beans and injecting them as required.

    One of the consequences is that bean injection / property setting might occur in a different order to what your XML wiring files would seem to imply. So you need to be careful that your property setters don't do initialization that relies on other setters already having been called. The way to deal with this is to declare beans as implementing the InitializingBean interface. This requires you to implement the afterPropertiesSet() method, and this is where you do the critical initialization. (I would also include code to check that important properties have actually been set.)