Spurious WARNINGs when XML declared TransactionProxyFactoryBean's target bean depends on an annotation declared bean that depends on another bean [SPR-16427] #20973
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
status: backported
An issue that has been backported to maintenance branches
type: bug
A general bug
Milestone
Andrew H opened SPR-16427 and commented
While trying to update some old XML configured code to use annotation configuration for bean declarations I started to see many WARNINGs about BeanCreationException in my log on startup.
Having spend quite some time trying to find where I had created a circular dependency in my beans, I realised that I hadn't and that the WARNINGs were all associated with XML declarations of TransactionProxyFactoryBean. I did some experimentation and created a small project to reproduce the issue in isolation.
Consider the following bean configuration:
transactionManager- A no-op dummy implementation of PlatformTransactionManager declared in XML
bazBean- A TransactionProxyFactoryBean whose target is bazBean_target
bazBean_target, A BazBean that can be declared with XML or with
@Component
, its constructor takes a BarBeanbarBean- A BarBean declared using
@Component
, its constructor takes a FooBeanfooBean - A FooBean declared using XML or
@Component
, its constructor takes no argumentsFor the example, none of these beans need have any methods or actually do anything. The BazBean and BarBean classes use constructor injection, and FooBean has no dependencies. It is necessary for BarBean to depend on another bean. If BarBean has no dependencies then the WARNING doesn't occur.
The above configuration (with bazBean_target in xml) will result in the following 1 warning being logged on startup:
WARNING: Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bazBean' defined in class path resource [context.xml]: Cannot resolve reference to bean 'bazBean_target' while setting bean property 'target'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bazBean_target' defined in class path resource [context.xml]: Cannot resolve reference to bean 'barBean' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'barBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
If you define bazBean_target using
@Component
you will get the following 2 warnings logged:_WARNING: Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bazBean' defined in class path resource [context.xml]: Cannot resolve reference to bean 'bazBean_target' while setting bean property 'target'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bazBean_target' defined in file [C:\dev\beancreationwarning\target\classes\com\example\beancreationwarning\beans\BazBean.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'barBean': Requested bean is currently in creation: Is there an unresolvable circular reference?
Jan 28, 2018 7:51:12 PM org.springframework.beans.factory.support.AbstractBeanFactory getTypeForFactoryBean
WARNING: Bean creation exception on non-lazy FactoryBean type check: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bazBean' defined in class path resource [context.xml]: Cannot resolve reference to bean 'bazBean_target' while setting bean property 'target'; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'bazBean_target': Requested bean is currently in creation: Is there an unresolvable circular reference?_
The beans do get created and work fine.
The order of annotated bean creation plays a role:
In the above example, if you declare another bean using
@Component
that depends on a BarBean and its classname comes alphabetically before BarBean - say "Aardvard" for example, then the warning on bazBean vanishes, but if you rename Aardvark to Zebra, the warning will return.I have not experimented to determine if the issue occurs with Java Config or with other types of ProxyFactoryBean. It is present in Spring 4.3.14 and 5.0.3 (and I would suspect earlier versions, but haven't verified)
Affects: 4.3.14, 5.0.3
Attachments:
Issue Links:
Referenced from: commits 65743a6, 7772b91
Backported to: 4.3.15
The text was updated successfully, but these errors were encountered: