Question

I have the following configuration:


@Aspect
public class MyAspect {

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

And have 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 am seeing that the behavior is not getting applied to @SomeAnnotation annotated method at runtime. Any idea why?

Thx.

Was it helpful?

Solution

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

If you're not sure try to debug into where you're using the class. You should see that the object isn't an instance of your class but a proxy object.

OTHER TIPS

Have you enabled AspectJ support?

You need to add

<aop:aspectj-autoproxy/>

to your bean context.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top