JAVA自定义注解

JAVA 自定义注解


注解概念

注解是 Java SE 5.0 版本开始引入的概念,它是对 java 源代码的说明,是一种元数据(描述数据的数据)。

注解和注释的不同

  • 注释

    注释是对代码的说明,给代码的读者看,便于帮读者梳理业务逻辑;在程序代码中经常看到的以 @ 开头的大部分是注解;

  • 注解

    注解也是对代码的说明,需要配合工具(解析它的代码)使用,参与代码的编译,给应用程序看的;

注解分类

注解以 @开头,我们会在应用程序中见到各种各样的注解,比如 @Autowired@Service@Controller@Override @Test@Value 等等,按照来源划分,可以分为 JDK 的注解第三方的注解自定义注解

JDK 注解

JAVA 内置注解

  • @Override (标记重写方法)
  • @Deprecated (标记过时)
  • @SuppressWarnings (忽略警告)

元注解 (注解的注解)

  • @Target (注解的作用目标)
  • @Retention (注解的生命周期)
  • @Document (注解是否被包含在 JavaDoc 中)
  • @Inherited (是否允许子类集成该注解)

第三方注解(各种框架注解)

请自行百度各个框架的注解详解

自定义注解

使用元注解自己定义的注解

JDK 中有一些元注解,主要有 @Target,@Retention,@Document,@Inherited 用来修饰注解。

@Target

表明该注解可以应用的 java 元素类型

Target 类型 描述
ElementType.TYPE 应用于类、接口(包括注解类型)、枚举
ElementType.FIELD 应用于属性(包括枚举中的常量)
ElementType.METHOD 应用于方法
ElementType.PARAMETER 应用于方法的形参
ElementType.CONSTRUCTOR 应用于构造函数
ElementType.LOCAL_VARIABLE 应用于局部变量
ElementType.ANNOTATION_TYPE 应用于注解类型
ElementType.PACKAGE 应用于包
ElementType.TYPE_PARAMETER 1.8 版本新增,应用于类型变量)
ElementType.TYPE_USE 1.8 版本新增,应用于任何使用类型的语句中(例如声明语句、泛型和强制转换语句中的类型)

@Retention

表明该注解的生命周期

生命周期类型 描述
RetentionPolicy.SOURCE 编译时被丢弃,不包含在类文件中
RetentionPolicy.CLASS JVM 加载时被丢弃,包含在类文件中,默认值
RetentionPolicy.RUNTIME 由 JVM 加载,包含在类文件中,在运行时可以被获取到

@Document

表明该注解标记的元素可以被 Javadoc 或类似的工具文档化

@Inherited

表明使用了 @Inherited 注解的注解,所标记的类的子类也会拥有这个注解

注解格式

/**
 * 修饰符 @interface 注解名 {
 * 注解元素的声明 1
 * 注解元素的声明 2
 * }
 * 修饰符:访问修饰符必须为 public, 不写默认为 pubic;
 * 关键字:必须为@interface;
 * 注解名: 注解名称为自定义注解的名称,使用时还会用到;
 * 注解类型元素:注解类型元素是注解中内容,可以理解成自定义接口的实现部分;
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyTestAnnotation {
    /**
     *	注解的元素声明的两种形式
     *	type elementName();
     *	type elementName() default value;  
     */
    String value() default "test";
}

注解样例

接下来我们以 Spring 中的 @Service 注解为例

@Target({ElementType.TYPE})// ElementType.TYPE 代表在注解上使用
@Retention(RetentionPolicy.RUNTIME)// RetentionPolicy.RUNTIME 代表运行时使用,可以通过反射获取到
@Documented// 包含在 JavaDoc 中
@Component// 允许通过包扫描的方式自动检测
public @interface Service {
<span class="hljs-comment">/**
 * The value may indicate a suggestion for a logical component name,
 * to be turned into a Spring bean in case of an autodetected component.
 * <span class="hljs-doctag">@return</span> the suggested component name, if any (or empty String otherwise)
 */</span>
<span class="hljs-meta">@AliasFor(annotation = Component.class)</span>
String <span class="hljs-title function_">value</span><span class="hljs-params">()</span> <span class="hljs-keyword">default</span> <span class="hljs-string">""</span>;

}

@Annotation

JDK1.5 有的,在 rt.jar 包下 java.lang.annotation包下,所有的注解默认继承了 Annotation 接口,但是它本身不能定义注解。

package java.lang.annotation;

/**

  • 所有的注解默认继承了 Annotation 接口,但是它本身不能定义注解。
  • The common interface extended by all annotation types. Note that an
  • interface that manually extends this one does <i>not</i> define
  • an annotation type. Also note that this interface does not itself
  • define an annotation type.
  • More information about annotation types can be found in section 9.6 of
  • <cite>The Java&trade; Language Specification</cite>.
  • The {@link java.lang.reflect.AnnotatedElement} interface discusses
  • compatibility concerns when evolving an annotation type from being
  • non-repeatable to being repeatable.
  • @author Josh Bloch
  • @since 1.5
    */
    public interface Annotation {
    .
    .
    .
    }

实现自定义注解

第一步 - 定义自定义注解

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyTestAnnotation {
    String value() default "test";
}

第二步 - 配置注解

@Data
@Builder
@MyTestAnnotation
public class MyBean {
    private String name;
    private int age;
}

第三步 - 利用反射解析注解

public class MyTest {
<span class="hljs-comment">//isAnnotationPresent:判断当前元素是否被指定注解修饰</span>
<span class="hljs-comment">//getAnnotation:返回指定的注解</span>
<span class="hljs-comment">//getAnnotations:返回所有的注解</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">main</span><span class="hljs-params">(String[] args)</span> {
    <span class="hljs-keyword">try</span> {
        <span class="hljs-comment">//获取MyBean的Class对象</span>
        <span class="hljs-type">MyBean</span> <span class="hljs-variable">myBean</span> <span class="hljs-operator">=</span> MyBean.builder().build();
        <span class="hljs-type">Class</span> <span class="hljs-variable">clazz</span> <span class="hljs-operator">=</span> myBean.getClass();
        
        <span class="hljs-comment">//判断myBean对象上是否有MyTestAnnotation注解</span>
        <span class="hljs-keyword">if</span> (clazz.isAnnotationPresent(MyTestAnnotation.class)) {
            System.out.println(<span class="hljs-string">"MyBean类上配置了MyTestAnnotation注解!"</span>);
            <span class="hljs-comment">//获取该对象上MyTestAnnotation类型的注解</span>
            <span class="hljs-type">MyTestAnnotation</span> <span class="hljs-variable">myTestAnnotation</span> <span class="hljs-operator">=</span> (MyTestAnnotation) clazz.getAnnotation(MyTestAnnotation.class);
            System.out.println(myTestAnnotation.value());
        } <span class="hljs-keyword">else</span> {
            System.out.println(<span class="hljs-string">"MyBean类上没有配置MyTestAnnotation注解!"</span>);
        }
    } <span class="hljs-keyword">catch</span> (Exception e) {
        e.printStackTrace();
    }
}

}

执行 main 方法,运行结果:

Connected to the target VM, address: '127.0.0.1:62125', transport: 'socket'
MyBean类上配置了MyTestAnnotation注解!
test
Disconnected from the target VM, address: '127.0.0.1:62125', transport: 'socket'

参考链接:https://blog.csdn.net/zt15732625878/article/details/99671586

参考链接:https://blog.csdn.net/zt15732625878/article/details/100061528

赵小胖个人博客