我正在尝试创建一个演示 AOP 应用程序,但它无法正常工作。 我通读了所有教程并让它与 @RestController 一起工作,但是当我使用普通的 java spring 驱动的应用程序尝试它时,我只是无法让它工作。请检查我的文件并告诉我错误所在。
应用程序类
@SpringBootApplication
@ComponentScan("com.xetra.experimental")
@EnableAspectJAutoProxy
public class AoptryoutnowebApplication {
public static void main(String[] args) {
SpringApplication.run(AoptryoutnowebApplication.class, args);
DefaultClassToAspectImpl defaultClassToAspectImpl = new DefaultClassToAspectImpl();
defaultClassToAspectImpl.doStuff();
}
}
ClassToAspect接口(interface)
public interface ClassToAspect {
void doStuff();
}
ClassToAspect 实现
@Component
public class DefaultClassToAspectImpl implements ClassToAspect {
@FooAnnotation
public void doStuff(){
System.out.println("DoStuff!");
}
}
切入点注释
public @interface FooAnnotation {
}
方面类
@Aspect
public class FooAspect {
@Pointcut("@annotation(FooAnnotation)")
public void methods(){
}
@Before("methods()")
public void doAspect(){
System.out.println("FooAspect before");
}
}
最佳答案
试试这个:
将
@EnableAspectJAutoProxy
替换为@EnableAspectJAutoProxy(proxyTargetClass = false)
将切入点更改为
@Pointcut("执行(* your.package..*.*(..)) && @annotation(fooAnnotation))")
关于java - Spring AOP 建议的方法不起作用。我的配置看起来没问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47220171/