Kilokahn
Kilokahn

Reputation: 2311

Calling of <aop:aspectj-autoproxy /> multiple times when loading spring context

Let's say I am defining a custom aspect and to enable proxying I am using aop:aspectj-autoproxy. Now I am also importing another third-party spring context in the application that also happens to call aop:aspectj-autoproxy (ofcourse I won't know about it upfront unless I pore over the context xml contents extracted from the JAR). Potentially there can be many such contexts. Here I see that the beans matching the pointcut get proxied over and over i.e. proxy of a proxy. Is there a way that one can avoid such a proxy of a proxy? Also feel free to point out any anti-patterns that may be at play here.

Thanks in advance.

Upvotes: 1

Views: 2102

Answers (2)

Ramesh
Ramesh

Reputation: 3881

From Spring documentation:

http://static.springsource.org/spring/docs/3.0.6.RELEASE/spring-framework-reference/html/aop.html#aop-proxying

Note Multiple sections are collapsed into a single unified auto-proxy creator at runtime, which applies the strongest proxy settings that any of the sections (typically from different XML bean definition files) specified. This also applies to the tx:annotation-driven and aop:aspectj-autoproxy elements.

To be clear: using 'proxy-target-class="true"' on , or elements will force the use of CGLIB proxies for all three of them

Upvotes: 2

Adrian Shum
Adrian Shum

Reputation: 40066

I believe the problem mainly relates to

Now I am also importing another third-party spring context in the application that also happens to call aop:aspectj-autoproxy

Instead of directly importing the app ctx provided by 3rd party lib (which is, in most case, provided for ease of initial development or for small project), try to manage those 3rd party beans in your own app ctx, and make sure you do not have multiple aop:aspectj-autoproxy (There are lots of bean post-processor etc in spring that is not safe in multiple declaration.)

Upvotes: 0

Related Questions