Spring 通知中的注释值

Spring 通知中的注释值,spring,aspectj,spring-aop,Spring,Aspectj,Spring Aop,我想在advice中访问我的注释值,我的注释可以放在type或method上。 到目前为止,我能够在方法上应用注释值,但在类型上应用注释时没有成功 @Before( value = "(@annotation(varun.mis.aspect.Logged) || within(@varun.mis.aspect.Logged *)) && (@annotation(logged))",argNames = "logged" ) 有什么建议吗?我不相信在应用于类型时,可以将注释

我想在advice中访问我的注释值,我的注释可以放在type或method上。 到目前为止,我能够在方法上应用注释值,但在类型上应用注释时没有成功

@Before( value = "(@annotation(varun.mis.aspect.Logged) || within(@varun.mis.aspect.Logged *)) && (@annotation(logged))",argNames = "logged" )

有什么建议吗?

我不相信在应用于类型时,可以将注释作为参数

下面的切入点表达式

@Before("@annotation(com.some.TestAnnotation) || within(@com.some.TestAnnotation *)")
将匹配带注释的方法或带注释的类。然后,您可以声明通知以从方法或类获取注释

MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
TestAnnotation annotation = methodSignature.getMethod().getAnnotation(TestAnnotation.class);
if (annotation == null) {
    Class<?> clazz = methodSignature.getDeclaringType();
    annotation = clazz.getAnnotation(TestAnnotation.class);
    if (annotation == null) {
        System.out.println("impossible");
    } else {
        System.out.println("class has it");
    }
} else {
    System.out.println("method has it");
}
MethodSignature MethodSignature=(MethodSignature)joinPoint.getSignature();
TestAnnotation annotation=methodSignature.getMethod().getAnnotation(TestAnnotation.class);
if(注释==null){
类clazz=methodSignature.getDeclaringType();
annotation=clazz.getAnnotation(TestAnnotation.class);
if(注释==null){
System.out.println(“不可能”);
}否则{
System.out.println(“类拥有它”);
}
}否则{
System.out.println(“方法有它”);
}

你也应该考虑方法和类型都有注释的情况。

直接使用注释如下:在<代码> >建议PARAMS < /Calp>和<代码> @注释(YouTalk) <代码> >周围< /代码>
@Around("execution(public * *(..)) && @annotation(yourAnnotation)")
public Object procede(ProceedingJoinPoint pjp, com.mycompany.MyAnnotation yourAnnotation) {
    ...
    yourAnnotation.value(); // get your annotation value directly;
    ...
}
通知参数中的
com.mycompany.MyAnnotation

@Around("execution(public * *(..)) && @annotation(com.mycompany.MyAnnotation)")
yourAnnotation
可以是有效的变量名,因为params中的
MyAnnotation
已经指出应该是哪个注释。此处
yourAnnotation
仅用于检索注释实例

如果要传递更多参数,可以尝试
args()