【问题标题】:Spring AOP by annotation pointcut annotation not retrieveSpring AOP通过注解切入点注解不检索
【发布时间】:2017-04-11 08:13:58
【问题描述】:

我正在使用 Spring AOP 拦截 @MyAnnotation 的注释方法。拦截没问题。但是,不幸的是,我没有到达我的注释实例。

我的注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
    String[] description();
}

我的配置方面

@Aspect
public class OAuthAspect {

    @Pointcut(value = "execution(public * *(..))")
    public void anyPublicMethod() {
    }

    @Pointcut(value = "@annotation(annotation)", argNames = "annotation")
    public void anyAnnotationMethod(MyAnnotation annotation) {
    }

    @Around(value = "anyPublicMethod() && anyAnnotationMethod(annotation)")
    public Object authorization(ProceedingJoinPoint pjp, MyAnnotation annotation) throws Throwable {
        //annotation is null
    }
}

示例切入点:

@Service
public class ContextService {
    @MyAnnotation(description = {"de1", "des2"})
    public String getAll() {
    }
}

我不明白为什么我无法检索注释的实例。

如果有人有想法?

电脑:已编辑

【问题讨论】:

    标签: spring-aop


    【解决方案1】:

    对我而言,ContextService 类甚至无法编译,因为您的注释中存在拼写错误:String[] descrition();(注意缺少的“p”)实际上应该是 String[] description();,然后它会编译,我也可以打印注释实例。

    【讨论】:

    • 对于“描述”只是转录错误。在我的项目中存在'p'并且我的项目编译,但注释实例为空:'(
    • 那你有配置问题。我用普通的 AspectJ 在我的机器上测试了这个方面,它可以工作。如果 Spring AOP 中没有,那一定是 config 问题。如果您不同意,请提供完整的MCVE,包括。 Maven 通过 GitHub 构建。
    • 好的,它工作正常,我用力清洁我的项目,没关系。我认为我在构建项目时遇到了问题。谢谢你的帮助:)
    • 你介意接受并支持我的回答吗?我指出了您的拼写错误,并确认这方面是可以的,并且问题一定出在您项目的其他地方。看来,我是对的。谢谢。
    猜你喜欢
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多