3

我有以下配置:


@Aspect
public class MyAspect {

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

并具有以下 bean 定义:

<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>

我看到该行为在运行时未应用于带@SomeAnnotation注释的方法。知道为什么吗?

谢谢。

4

2 回答 2

2

您是否启用了 AspectJ 支持

您需要添加

<aop:aspectj-autoproxy/>

到您的 bean 上下文。

于 2009-11-06T20:49:22.307 回答
2

确保带有 @SomeAnnoation 的类是由 Spring 容器创建的。Spring 通过创建代理类来包装对象,将 AOP 应用于从容器中检索的类。然后,此代理类在调用该对象的方法之前和之后执行 Aspect。

如果您不确定尝试调试到您使用该类的位置。您应该看到该对象不是您的类的实例,而是代理对象。

于 2009-11-06T21:58:07.003 回答