views:

212

answers:

2

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"&gt;
    <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.

+1  A: 

Have you enabled AspectJ support?

You need to add

<aop:aspectj-autoproxy/>

to your bean context.

ChssPly76
Yes I have..i have one more bean definition file in the DAO where I am doing this stuff..Though the code I posted is from the Service layer.. Do I need to run the auto proxy again??
peakit
As far as I know, it has to be done per context. So unless the other file **included** in this one (or vice versa) OR you're using hierarchical contexts and this file is **higher** in hierarchy then the other, you need to run auto proxy again.
ChssPly76
Yes both the files finally come into the 'same' spring context..And even I have tried adding the auto proxy creator it did n't help.. i am still not seeing any behavior to be applied.
peakit
Hmm... the annotated method is public, right? And the bean in question is declared in context? I'm not nitpicking on things like `@annotation(SomeAnnotation)` being in quotes and `joinPoint.proceed()` not being invoked as I'm assuming those are the copy/paste artifacts, but do make sure your actual code is accurate.
ChssPly76
+1  A: 

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.

Jason Gritman
+1 for debugging - good sanity check.
ChssPly76