java - Understanding Spring context initialization order -
i have complex set of beans , dependencies between them. beans @service, @repository or @controller annotated , use @postconstruct annotation. there circular dependencies still system correctly initialized spring.
then added simple controller dependency 1 of services. theoretically, system should able boot because theoretically first set system before , new controller. spring complains cannot set context:
error creating bean name 'userservice': requested bean in creation: there unresolvable circular reference?
can somehow assists spring in how order context initialization? think main issue userservice used lot through system authentication purposes.
best solution take out circular dependency; have not yet encountered scenario such structure warranted. if want stick perhaps above problem due fact have constructor injection somewhere:
circular dependencies if using predominantly constructor injection possible write , configure classes , beans such unresolvable circular dependency scenario created. consider scenario have class a, requires instance of class b provided via constructor injection, , class b, requires instance of class provided via constructor injection. if configure beans classes , b injected each other, spring ioc container detect circular reference @ runtime, , throw beancurrentlyincreationexception.
one possible solution issue edit source code of of classes configured via setters instead of via constructors. solution not use constructor injection , stick setter injection only. in other words, while should avoided in rarest of circumstances, possible configure circular dependencies setter injection. unlike typical case (with no circular dependencies), circular dependency between bean , bean b force 1 of beans injected other prior being initialized (a classic chicken/egg scenario).
Comments
Post a Comment