开发者

Spring aop intercepting calls from within the same service class

开发者 https://www.devze.com 2023-04-03 02:32 出处:网络
I have a same scenario as mentioned in Spring @Transaction method call by the method within the same class, does not work?

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 wel开发者_如何学JAVAl 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消