Spring为所有方法创建一个带有元注释中包含注释的方法的方面

问题描述 投票:0回答:1

我有一个这样的注释(在Spring Boot 2中:)>

package com.test;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface MyAnnotation { }

和元注释:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@MyAnnotation 
public @interface MyEndpoint{ }

如何定义一个方面以便在具有@MyAnnotation的每个方法之前执行?

如果我在下面定义的话,

@Before("execution(public * *(..)) && @annotation(myAnnotation)")
public void authorize(JoinPoint pjp, MyAnnotation myAnnotation) { }

然后仅调用方法authorize

@MyAnnotation()
public myMethod(){}

并且在这种情况下不是:

@MyEndpoint()
public myMethod() {}

谢谢

我有一个这样的注释(在Spring Boot 2中):package com.test; @Target({ElementType.METHOD,ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@继承的公共@interface MyAnnotation {} ...

spring-boot annotations aop aspectj spring-annotations
1个回答
0
投票

@Inherited批注仅适用于从带注释的类到子类,在任何其他情况下均不适用。我对此进行了详细的解释here,还显示了AspectJ解决方法的外观。您可以根据情况进行调整。 但是

© www.soinside.com 2019 - 2024. All rights reserved.