开发者

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 @SomeAnnotat开发者_如何学编程ion annotated method at runtime. Any idea why?

Thx.


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.


Have you enabled AspectJ support?

You need to add

<aop:aspectj-autoproxy/>

to your bean context.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜