Spring aop intercepting calls from within the same service class

不打扰是莪最后的温柔 提交于 2019-12-06 02:42:31

问题


I have a same scenario as mentioned in

Spring @Transaction method call by the method within the same class, does not work?

I was referring to answer #1 which i thought would work for my simple pojo class but it didnt. In my case i dont have annotation @Transaction. Its a simple pojo class. I wish to intercept each method adduser as well as addusers using spring aop if i take example in post above.

Is it possible to intercept method that is being called from the same service call? I was referring to AspectJAwareProxy which does the trick but doesnt resolve the issue as a whole. What i mean is i dont want anything to be added to my business logic. So i want to avoid any coding except for defining pointcut and defining the advice. Is it something possible using Java and spring aop? I am using CGlib to generate proxy. Spring version is 3.0.5.Release.

Thanks, Ajay


回答1:


For this to work, you have to use load-time weaving instead of proxying. The reason is because spring uses proxying to achieve AOP functionality (such as transaction support). Once inside a class instance, any method-calls to methods in the same instance will be directly against the actual instance object, not the wrapping proxy so AOP advices will not be considered. Load-time weaving works differently. There, you have an outside java agent that manipulates the classes on a byte-code level to inject the AOP support.

You will need to

1: Modify your java command-line used to include the spring aspectj agent

2: add

<context:load-time-weaver aspectj-weaving="on" />
<tx:annotation-driven mode="aspectj" />

to your spring cofig.

Read more:

AspectJ Load Time Weaving with Spring Transaction Manager and Maven

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-aj-ltw



来源:https://stackoverflow.com/questions/7338760/spring-aop-intercepting-calls-from-within-the-same-service-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!