John Doe opened SPR-16114 and commented
When using cglib proxy, 'this' should be the proxy (like for the @Configuration
classes), making internal calls eligible for interception. It is how cglib Enhancer works, @Configuration
also works this way but not @Component
beans (nor objects created from @Import
or @Bean
).
if there is a good reason for this different behaviors maybe it should be documented.
No further details from SPR-16114
Comment From: spring-projects-issues
Juergen Hoeller commented
This is simply due to the proxy nature of Spring AOP: Like with an interface-based proxy, a CGLIB proxy is a separate instance in front of the actual target object. This allows for a decoupled lifecycle, e.g. lazy initialization or prototype/request/session scoping for the target object, with a stable proxy instance as a front facade that can be shared and injected anywhere.
You got a point that, in principle, we could also provide an interception model based on runtime-generated CGLIB subclasses which replace the original bean class instead of serving as a proxy, just supporting interceptors and no individual target lifecycle. However, that would be yet another model with distinct characteristics, in addition to the target-class proxy model... and also in addition to AspectJ weaving (modifying the bytecode of the original bean class without proxying or subclassing and therefore providing the desired 'this' semantics in a different way) which we're supporting for several Spring features already.