Spring boot introductory tutorial-custom annotations

table of Contents

1.pom.xml add aop support

2. Create custom annotations

3. Achieve annotations through aspects

3.1 Get the variables in the annotation 

3.2 Description of ProceedingJoinPoint and JoinPoint in annotations


 

1.pom.xml add aop support

<!-- 引入aop切面支持 -->
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2. Create custom annotations

@Documented
@Retention(RUNTIME)
@Target(METHOD)
public @interface MyLog {

    String value() default "";

}
@Documented 注解: Documented 注解表明这个注解应该被 javadoc工具记录. 默认情况下,javadoc是不包括注解的.但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的工具处理, 所以注解类型信息也会被包括在生成的文档中

@Inherited 注解: 它指明被注解的类会自动继承. 更具体地说,如果定义注解时使用了 @Inherited 标记
然后用定义的注解来标注另一个父类, 父类又有一个子类(subclass),则父类的所有属性将被继承到它的子类中

@Target(ElementType.TYPE)   //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR)  //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包   

1.RetentionPolicy.SOURCE —— 这种类型的Annotations只在源代码级别保留,编译时就会被忽略

2.RetentionPolicy.CLASS —— 这种类型的Annotations编译时被保留,在class文件中存在,但JVM将会忽略
3.RetentionPolicy.RUNTIME —— 这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用.

3. Achieve annotations through aspects



@Component
@Aspect
public class LogAspect {

@Pointcut("@annotation(com.example.config.MyLog)")
private void cut() { }




@Around("cut()")
public void advice(ProceedingJoinPoint joinPoint){

    System.out.println("环绕通知之开始");

    try {
    joinPoint.proceed();
} catch (Throwable e) {
    e.printStackTrace();
}

System.out.println("环绕通知之结束");

}



//当想获得注解里面的属性,可以直接注入改注解

@Before("cut()&&@annotation(myLog)")
public void record(JoinPoint joinPoint, MyLog myLog) {
    System.out.println(myLog.value());

}



@After("recordLog()")
public void after() {
    this.printLog("已经记录下操作日志@After 方法执行后");

}

}

 

3.1 Get the variables in the annotation 

//当想获得注解里面的属性,可以直接注入改注解

@Before("cut()&&@annotation(myLog)")
public void record(JoinPoint joinPoint, MyLog myLog) {
    System.out.println(myLog.value());

}

3.2 ProceedingJoinPointand JoinPointdescription in annotations

AspectJ uses the org.aspectj.lang.JoinPoint interface to represent the connection point object of the target class. If it is surround enhancement, it uses org.aspectj.lang.ProceedingJoinPoint to represent the connection point object, which is a sub-interface of JoinPoint. Any enhancement method can access the information of the connection point context by declaring the first input parameter as JoinPoint.

The main methods of these two interfaces: 

  • JoinPoint  
java.lang.Object[] getArgs():获取连接点方法运行时的入参列表; 
Signature getSignature() :获取连接点的方法签名对象; 
java.lang.Object getTarget() :获取连接点所在的目标对象; 
java.lang.Object getThis() :获取代理对象本身; 
  • ProceedingJoinPoint  
ProceedingJoinPoint继承JoinPoint子接口,它新增了两个用于执行连接点方法的方法: 
java.lang.Object proceed() throws java.lang.Throwable:通过反射执行目标对象的连接点处的方法; 
java.lang.Object proceed(java.lang.Object[] args) throws java.lang.Throwable:通过反射执行目标对象连接点处的方法,不过           使用新的入参替换原来的入参。 

Guess you like

Origin blog.csdn.net/qq_27828675/article/details/107664958