spring 自定义注解


在项目中,有非常多的场景需要用到自定义注解,当然出去面试也必然会被问到spring 如何实现自定义注解。项目中的请求日志,定时任务日志,部分接口限流请求等需要用到自定义注解

  • 声明注解

package com.coderpwh.note.note;

import java.lang.annotation.*;

/**
 * @author coderpwh
 * @create 2020-03-06 15:46
 * @desc ${DESCRIPTION}
 **/

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CoderLog {

String  value() default "this is log Are you ok?";

}

@Documented:是否将注解添加到java文档中去

@Retention:该注解的生命的周期:

  1. SOURCE(编译阶段丢弃,不会写入字节码) ,
  2. CLASS(类加载阶段,在类加载的时候丢弃,在字节码文件的处理中有用。注解默认使用这种方式。),
  3. RUNTIME(始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。自定义的注解通常使用这种方式)

@Target:注解用到什么地方,ElementType.METHOD 用在方法上面,更多枚举值可以参考ElementType的源码

  • 注解的实现
package com.coderpwh.note.note;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author coderpwh
 * @create 2020-03-06 16:51
 * @desc ${DESCRIPTION}
 **/
@Component
@Aspect
public class CoderLogAspect {

    @Pointcut("@annotation(com.coderpwh.note.note.CoderLog)")
    private void pointcut() {
    }

    
    @Before("pointcut() &&@annotation(logger)")
    public void advice(CoderLog logger) {
        System.out.println(" ---- 哥写的日志内容为:[ " + logger.value() + "] -----");
    }


}
  1. @Componet:将实例注解到容器中
  2. @Aspect:表示切面容器读取
  3. @Pointcut: Pointcut是植入Advice的触发条件。每个Pointcut的定义包括2部分,一是表达式,二是方法签名。方法签名必须是 public及void型。可以将Pointcut中的方法看作是一个被Advice引用的助记符,因为表达式不直观,因此我们可以通过方法签名的方式为 此表达式命名。因此Pointcut中的方法只需要方法签名,而不需要在方法体内编写实际代码。
  4. @Before:标识一个前置增强方法,相当于BeforeAdvice的功能,相似功能的还有

run一下,访问接口:http://localhost:8080/user/get/userInfo?userName=hello

结果如下:

代码地址:https://github.com/coder-PengWenHao/Spring-Boot-Cloud-demos/tree/master/spring%20boots/customNote


文章作者: coderpwh
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 coderpwh !
  目录