Spring - Aspect not applicable at run time

I have the following configuration:


@Aspect
public class MyAspect {

 @Around(@annotation(SomeAnnotation))
 public Object myMethod(ProceedingJoinPoint joinPoint) throws Throwable {
   System.out.println("Hello...");
 }
}

And the following beans definitions:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    <bean id="myAspect" class="MyAspect" />
</beans>

I see that the behavior does not apply to the annotated method @SomeAnnotationat runtime. Any idea why?

thanks.

+3
source share
2 answers

Make sure the class with @SomeAnnoation is created by the Spring container. Spring applies AOP to classes that are retrieved from the container, creating a proxy class to wrap the object. This proxy class then executes the Aspect methods before and after the methods of this object are called.

, , . , , -.

+2

AspectJ?

<aop:aspectj-autoproxy/>

bean.

+2

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


All Articles