使用Target = ElementType.TYPE创建用于注释的AspectJ(类注释)

我想对带有自定义注释的带注释类的所有方法执行简单的日志。我创建了下一个注释:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface FooAnnotation {}

它用于随机类:

@FooAnnotation
public class Bar{
  ...
}

当然,我的入门班有@EnableAspectJAutoProxy批注

@EnableAspectJAutoProxy
@SpringBootApplication
public class FooApplication {
  public static void main(String[] args) {
    SpringApplication.run(FooApplication.class,args);
  }
}

在相关的AspectJ中,我需要定义一个 @Pointcut ,以对用 @FooAnnotation 注释的所有句柄的所有方法执行 @After >

我尝试了下一个AspectJ,它不起作用

@Slf4j
@Aspect
@Component
public class FooAspectJ{

  @Pointcut("within(x.y.z.FooNotify)")
  private void annotatedWithin() {}

  @Pointcut("@within(x.y.z.FooNotify)")
  private void annotatedAtWithin() {}

  @Pointcut("@annotation(x.y.z.FooNotify)")
  private void annotatedAtAnnotation() {}


  @After("annotatedWithin() || annotatedAtWithin() || annotatedAtAnnotation()")
  private void afterAnnotation(JoinPoint joinPoint){
    log.info("Executed annotated {}",joinPoint.getSignature().getName());
  }

}

我看到了与此类似的帖子,例如 @AspectJ pointcut for all methods of a class with specific annotationaspectj pointcut with annotation parameters,结果相同。

ruanhuibao 回答:使用Target = ElementType.TYPE创建用于注释的AspectJ(类注释)

应该像

@Pointcut("within(@x.y.z.FooNotify *)")

示例:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

@FooNotify
@Repository
public class Bar {
  private static final Logger logger = LoggerFactory.getLogger(Bar.class);

  public String returnSomeString() {
    logger.info("returnSomeString ...");
    int i = returnMeAnInt();
    logger.info("returnMeAnInt returned {}",i);
    return "Hello";
  }

  public void doSomething() {
    logger.info("doSomething ...");
  }

  private int returnMeAnInt() {
    logger.info("returnMeAnInt ...");
    return 4;
  }
}

方面:

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class BarAspect {
  private static final Logger logger = LoggerFactory.getLogger(BarAspect.class);

  @Pointcut("within(@FooNotify *)")
  public void fooNotifyPointcut() {}

  @After("fooNotifyPointcut()")
  public void afterAnnotation(JoinPoint jp) {
    logger.info("afterAnnotation joinpoint: {}",jp.getStaticPart().getSignature());
  }
}

注释:

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Retention(RUNTIME)
@Target(TYPE)
public @interface FooNotify {}

致电:

 bar.returnSomeString();
    bar.doSomething();

日志:

  

Bar:13- returnSomeString ...

     

Bar:24- returnMeAnInt ...

     

Bar:15- returnMeAnInt返回4

     

BarAspect:21- afterAnnotation连接点:字符串   com.tierpoint.api.vcloud.Bar.returnSomeString()

     

Bar:20- doSomething ...

     

BarAspect:21- afterAnnotation连接点:无效   com.tierpoint.api.vcloud.Bar.doSomething()

对于私有方法,您必须使用完整的aspectj。

本文链接:https://www.f2er.com/3073711.html

大家都在问