自定义java注解

 原文章

 https://blog.csdn.net/xsp_happyboy/article/details/80987484 

java自定义注解

Java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。
注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 java.lang.annotation 包中。

1、元注解

元注解是指注解的注解。包括  @Retention @Target @Document @Inherited四种。

(1)自定义简单注解

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

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

    //方法默认值

    String value() default "";
    
}

(2)使用注解

在类上使用@Token("testToken")

(3)校验是否使用注解

HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
Token token = method.getAnnotation(Token.class);

//调用注解的方法

token.value();

if(StringUtil.isNotEnpty(token.value())){

     //处理业务逻辑

}

猜你喜欢

转载自blog.csdn.net/yc62326/article/details/85262872