Skip to content

Objects created by FactoryBeans might get processed multiple times by BeanPostProcessors if beans are created lazily [SPR-11937] #16554

Closed
@spring-projects-issues

Description

@spring-projects-issues

Andreas Benneke opened SPR-11937 and commented

We wondered why the ScheduledAnnotationBeanPostProcessor schedules some of our jobs twice - and tracked this down to this situation.

The attached test case is a very simplified version:

  • Our beans are created lazily.
  • We have a set of beans which form a circle (BeanImpl1 requires BeanImpl2 and vice versa).
  • There are two instances of each bean in the application context, where one is the real implementation.
  • The other is a Proxy (created by the ProxyFactoryBean in real life, but a simple stripped down SimpleFactoryBean is sufficient in this case).
  • The Proxies are marked as primary=true and used for injection.
  • We added a TestPostProcessor counting the number of invocations of postProcessAfterInitialization per beanName for testing purposes.

The test cases reproduce simpler as well as eagerly created versions of this situation:

The TestPostProcessor is invoked only once per bean (as expected), if

  • everything is not created lazily (TestEager*).
  • there is no circle (not covered in the test cases).
  • there are no proxies and everything is defined in XML (TestLazyXml, TestEagerXml).
  • there are no proxies and everything is autowired (TestLazyAutowired, TestEagerAutowired).

But it is invoked twice (and the tests fail), if the proxies and objects are created lazily (TestLazyAutowiredFactoryBean, TestLazyXmlFactoryBean).

While tracing down this situation, we noticed that the isCurrentlyInCreation-logic is only applied to the FactoryBeans itself but not to the objects created by those factories - which might have helped to avoid this duplicate invocation of the BeanPostProcessor ...?


Affects: 3.2.9, 4.0.5

Attachments:

Backported to: 3.2.10

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