peakit
peakit

Reputation: 29349

Spring - Aspect is not getting applied at runtime

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.

Upvotes: 3

Views: 3221

Answers (2)

Jason Gritman
Jason Gritman

Reputation: 5301

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.

Upvotes: 2

ChssPly76
ChssPly76

Reputation: 100686

Have you enabled AspectJ support?

You need to add

<aop:aspectj-autoproxy/>

to your bean context.

Upvotes: 2

Related Questions