Spring AOP : Annotation on any method called x not working

标签 spring aop spring-aop

我是第一次开始使用 AOP。

我的第一个方面如下:

@Aspect
public class SyncLoggingAspect {
    private final Logger logger = Logger.getLogger(this.getClass());

    @Before("execution(public * *(..))")
    public void anyPublic() {
        System.out.println("HIT POINTCUT");
    }
}

这成功地在任何公开的方法调用上被调用。但是,当我将其更改为:

@Before("execution(public * doPoll(..))")
public void anyPublic() {
    System.out.println("HIT POINTCUT");
}

我希望它适用于任何名为“doPoll”的公共(public)方法,但是当调用这样的方法时,什么都不会发生:

public class GmailContactPoller extends ContactPoller<GoogleUser, ContactPusher<GoogleUser>> {
    Logger logger = Logger.getLogger(this.getClass());

    @Override
    public List<? extends ContactPusher<GoogleUser>> doPoll() throws PollException {
           ...
    }
}

EL 语法有什么遗漏吗?还是这与继承层次结构有关? doPoll 的父类(super class)方法在称为 Poller 的抽象类中是抽象的。没有接口(interface)会不会出问题?

编辑:我刚刚注意到我的 IDE 启用了 spring aspect 工具,现在我通过该方法收到以下编译器警告:

"描述资源路径位置类型 datasync.aop.aspects.SyncLoggingAspect 中定义的建议尚未应用 [Xlint:adviceDidNotMatch] SyncLoggingAspect.java/DataSync/src/main/datasync/aop/aspects"

最佳答案

Spring AOP Proxies 和 aspectJ 主要有一些区别:

  • Spring AOP 仅适用于公共(public)方法。
  • Spring AOP 不适用于自调用。

您可以查看sections 8.4 & 8.5 of Spring's Documentation了解更多信息。

目前你有两种解决方案:

  1. 重构您的代码以消除 self 调用的需要
  2. 在编译时或加载时使用 AspectJ 而不是 Spring AOP 代理。

关于 Spring AOP : Annotation on any method called x not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9126433/

相关文章:

spring - 在 Spring Boot 中 Jackson 之前验证输入

aop - Spring AOP中切面、关注点和横切关注点的区别

java - 访问 spring bean 代理引用本身

java - AOP 使用Around 避免执行一个方法

java - PSQL异常 : ERROR: duplicate key value violates unique constraint when using Hibernate

java - HashCode 循环调用 : OneToMany JPA

php - Yii 2 有 AOP 吗?

java - 编织一个没有接口(interface)和非公共(public)方法的类时关于理解Spring AOP的问题

java - 两次调用 Spring AOP 建议

java - 为什么我无法在 Spring-5.0.0.RELEASE 中导入 ApplicationContext