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_PARAMETER1.8版本新增,应用于类型变量)
ElementType.TYPE_USE1.8版本新增,应用于任何使用类型的语句中(例如声明语句、泛型和强制转换语句中的类型)

@Retention

表明该注解的生命周期

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

@Document

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

@Inherited

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

注解格式

/** * 修饰符 @interface 注解名 { * 注解元素的声明1 * 注解元素的声明2 * } * 修饰符:访问修饰符必须为public,不写默认为pubic; * 关键字:必须为@interface; * 注解名: 注解名称为自定义注解的名称,使用时还会用到; * 注解类型元素:注解类型元素是注解中内容,可以理解成自定义接口的实现部分; */@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @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 {    /**     * The value may indicate a suggestion for a logical component name,     * to be turned into a Spring bean in case of an autodetected component.     * @return the suggested component name, if any (or empty String otherwise)     */    @AliasFor(annotation = Component.class)    String value() default "";}

@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)@Documentedpublic @interface MyTestAnnotation {    String value() default "test";}

第二步-配置注解

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

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

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

执行main方法,运行结果:

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

参考链接:https://blog.csdn.net/zt15732...

参考链接:https://blog.csdn.net/zt15732...

赵小胖个人博客