Spring AOP Proxy Not Working with BeanPostProcessor and FactoryBean Dependencies #34563
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
status: waiting-for-triage
An issue we've not yet triaged or decided on
Description
When using Spring AOP to proxy a HelloService bean in combination with a custom BeanPostProcessor (MyPostProcessor) and a FactoryBean (MyFactoryBean), the AOP proxy is not applied to the target bean. The HelloService bean is returned as a plain Java object without any proxy applied. This issue occurs when the BeanPostProcessor has a dependency on another bean (GoodByeService in this case).
Expected Behavior
The HelloService bean should be proxied by AOP, and the following checks should return true:
AopUtils.isAopProxy(helloService)
AopUtils.isJdkDynamicProxy(helloService) or AopUtils.isCglibProxy(helloService)
The @AfterReturning advice in MyAspect should also be invoked when calling the sayHello method of HelloService.
Actual Behavior
The HelloService bean is not proxied. The following checks return false:
AopUtils.isAopProxy(helloService)
AopUtils.isJdkDynamicProxy(helloService)
AopUtils.isCglibProxy(helloService)
Additionally, the @AfterReturning advice in MyAspect is not invoked.
Steps to Reproduce
Here is a minimal reproducible example:
Analysis and Workaround
The issue can be resolved by either of the following:
Question
Why does the presence of a BeanPostProcessor with dependencies interfere with the AOP proxy creation for HelloService? Is this a bug in Spring, or is there a specific configuration requirement that I missed?
Additional Context
This issue seems related to the bean initialization order or proxy creation timing, especially when BeanPostProcessor and FactoryBean dependencies are involved. Further clarification would be appreciated.
The text was updated successfully, but these errors were encountered: