Aspectj and spring with aspectj-autoproxy

I declared my aspects using the @Aspect annotation, but the tip does not seem to apply. The aspect works in several other projects that I have, and the main difference is that the other projects are fully connected via annotations, and this particular project is wired xml. The only bean that is an annotation is Aspect. So I'm wondering if spring aspectj supports when using aspectj-autoproxy is sensitive to the order in which beans are defined in xml.

For example, beans will be declared after aspectj-autoproxy in xml is considered for AOP points?

EDIT:

I moved <aop:aspectj-autoproxy /> until all beans are created and still fail.

Basically my code consists of:

 @Component @Aspect public class SomeAspect { @Pointcut("@annotation(MyAnnotation)") public void isX() {} @After("isX()") public void XX() { System.out.println("Called aspect"); } } 

And my controller has something like:

 public class XController extends AbstractCommandController { @MyAnnotation public void handleX(...) { // do stuff } @Override protected void handle(...) { return handleX(...); } } 

And then spring xml:

 <context:component-scan base-package="package.of.some.aspect" /> <aop:aspectj-autoproxy /> <!-- the rest of the beans below --> <bean id="someController" class="..." /> 

My previous projects captured and loaded all beans using component scanning. What is different this time.

EDIT2: Another difference is that other projects use @Controller and @RequestMethod. And in this case, I am using the derived class AbstractCommmandController. I am wondering if this applies: http://forum.springsource.org/archive/index.php/t-46637.html

Namely, that I cannot seek advice from any method other than handleRequest ().

EDIT3:

My last attempt was to override handleRequest () and apply my annotation there. Under the assumption that when spring proxies my controller, it will see the annotation and apply the advice, since it calls through a public method called from the outside. This still does not work.

+6
source share
1 answer

I see that you are calling the handleX method directly from another method in the same class. This will not take into account the annotation, since the work of processing AOP annotations is done using the JDK proxy, which wraps your class and provides the same interfaces.

You may be able to get around this by using CGLIB instead of JDK proxies, but in my experience, the most reliable solution is to simply not rely on any AOP annotations for internal methods.

+3
source

Source: https://habr.com/ru/post/890780/


All Articles