Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring aop intercepting calls from within the same service class

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

like image 643
Ajay Avatar asked Nov 13 '22 17:11

Ajay


1 Answers

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

like image 75
pap Avatar answered Nov 16 '22 16:11

pap