Skip to content

Spurious WARNINGs when XML declared TransactionProxyFactoryBean's target bean depends on an annotation declared bean that depends on another bean [SPR-16427] #20973

Closed
@spring-projects-issues

Description

@spring-projects-issues

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 BarBean

barBean- A BarBean declared using @Component, its constructor takes a FooBean

fooBean - A FooBean declared using XML or @Component, its constructor takes no arguments

For 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

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions