要在 类 中的方法上使用的方面,该方法具有位于另一个注释内的注释

Aspect to use on methods in classes that has annotation that is inside another annotation

我有服务 classes 扩展具有 @Annotation1 注释的 BaseService class。在@Annotation1 中还有另一个注解 - @Annotation2.

我希望能够创建方面方法,运行s 在 classes 中的所有方法之前都用 @Annotation2 注释,因此在任何扩展 BaseService class 的服务中。 =16=]

当我直接在 BaseService 中应用 @Annotation2 时,我能够 运行 方面方法,或者当我直接在服务上应用 @Annotation1 时,但当 @Annotation2 在扩展中的 @Annotation1 内时不能 class

@Annotation1
abstract class BaseService {
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
@Annotation2
public @interface Annotation1 {
}
@Target(value = {ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Annotation2 {
}
public class TestService extends BaseService {

    private void testMethod(){
        // does some service task
    }
}
@Aspect
public class TestAspect {

    @Before("@within(com.example.Annotation2) ||" +
            " within(@(@com.example.Annotation2 *)*)" +
            " @annotation(com.example.Annotation2)")
    public void aspectMethod(JoinPoint joinPoint) {
        // should run before testMethod 
    }
}

方面方法 aspectMethod() 应该 运行 在 TestService 中的 testMethod() 之前 但没有。

根据 ,我希望这样的方面能够起作用:

package de.scrum_master.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class TestAspect {
  @Before(
    "execution(* *(..)) && (" +
      "within(@de.scrum_master.app.Annotation2 *) || " +
      "within(@(@de.scrum_master.app.Annotation2 *) *) || " +
      "within(@(@(@de.scrum_master.app.Annotation2 *) *) *)" +
    ")"
  )
  public void myAdvice1(JoinPoint joinPoint) {
    System.out.println(joinPoint);
  }
}

不幸的是它没有,这似乎是 AspectJ 的限制。我为此创建了 Bugzilla ticket #549437,您可能想要关注它。

解决方法:直接注释 TestService