注解 annotation

1、springboot的第一个注解

@Target(ElementType.TYPE) //标注在类上的注解

@Retention(RetentionPolicy.RUNTIME)//注解信息会保留到运行时

@Documented //制作javadoc时,注解信息也会生成到doc.html

@Inherited //如果某个类A应用了这个注解,那么其子类B会继承这个注解(如果B没有覆盖A的方法)

@SpringBootConfiguration --配置注解,这也是一个组合注解

@EnableAutoConfiguration  --启动自动配置,自动配置参数,比如log的参数,比如web服务器的参数

@ComponentScan(excludeFilters = {--扫描配置,Spring Boot默认会扫描@SpringBootApplication所在类的同级包以及它的子包。

@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),

@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

public @interface SpringBootApplication {

1.1 注解基础

一个注解,可以有多个属性,使用注解时,IDE要求设置每个属性值(有默认值的属性,不需要设置)

属性一般是简单类型,比如字符串、数字。也可以是类、其它注解、数组等,例如@ComponentScan

Value属性,可以省略属性名,直接这样写  @ABC(xxx),等价于@ABC(value=xxx)

所以有的注解就设计了两个功能相同的属性,比如value prefix

/**

 *

 * Retention(中文意思是‘保留)注解说明,这种类型的注解会被保留到那个阶段. 有三个值:

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

 * 2.RetentionPolicy.CLASS —— 这种类型的Annotations编译时被保留,在class文件中存在,但JVM将会忽略

 * 3.RetentionPolicy.RUNTIME ——

 * 这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用.

 *

 *

 ** @Target 表示该注解用于什么地方,可能的 ElemenetType 参数包括:

 *ElemenetType.CONSTRUCTOR 构造器声明

 *ElemenetType.FIELD 域声明(包括 enum 实例) ElemenetType.LOCAL_VARIABLE 局部变量声明

 *ElemenetType.METHOD 方法声明 ElemenetType.PACKAGE 包声明

 *ElemenetType.PARAMETER 参数声明

 *ElemenetType.TYPE 类,接口(包括注解类型)或enum声明

 */

@Retention(value = RetentionPolicy.RUNTIME)

@Target(value = { ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.FIELD })

public @interface Test_Retention {

    String doTestRetention(); //这个方法,像是一个属性字段,使用注解时,需要set一个字符串,读取注解信息时,也可以读到这个属性的值

}

 

@Test_Value("44444")

public class TestAnnotations {

 

    public static void main(String[] args) {

        try {

            Test_Value annotation_test_value = TestAnnotations.class.getAnnotation(Test_Value.class);

            System.out.println("annotation_test_value.value() : " + annotation_test_value.value());

            

            Method method_f1 = TestAnnotations.class.getDeclaredMethod("f2");

            System.out.println("method_f1 : " + method_f1);

            

            Test_Retention annotation_test_Retention = method_f1.getAnnotation(Test_Retention.class);

            System.out.println("annotation_test_Retention:" + annotation_test_Retention);

            System.out.println("annotation_test_Retention.doTestRetention():" + annotation_test_Retention.doTestRetention());

            

        }catch(Exception e) {

            e.printStackTrace();

        }

    }

    

    @Test_Documented(doTestDocument = "ddd")

    @Test_Value("333")

    public void f1() {

        System.out.println("f1()");

    }

    

    @Test_Retention(doTestRetention = "uuu")

    public void f2() {        

    }

}

 

/**

 * @Inherited 允许子类继承父类中的注解

 * 但如果子类覆盖了父类的方法,则这个方法的注解无法继承

 * 测试效果如下

 */

public class TestInherited {

 

    public static void main(String[] args) {

        try {

            //getDeclaredMethod获取类自身的public、private、以及interface中的方法。不包括父类的public方法。

            //getMethod获取类的所有public方法,包括继承来的public方法

            

            Test_Value annotation_test_value = B.class.getAnnotation(Test_Value.class);

            System.out.println("annotation_test_value.value() : " + annotation_test_value.value());

                        

            annotation_test_value =  B.class.getMethod("f1").getAnnotation(Test_Value.class);

            System.out.println("annotation_test_value.value() : " + annotation_test_value.value());

            

        }catch(Exception e) {

            e.printStackTrace();

        }

        

        System.out.println("=================");

        try {

            Thread.sleep(100);

            //注掉B中对f2的覆盖,getMethod可以找到f2方法

            //保留B中对f2的覆盖,getMethod可以找到f2方法,但获取不到继承的注解信息

            Test_Value annotation_test_value =  B.class.getMethod("f2").getAnnotation(Test_Value.class);

            System.out.println("getMethod--annotation_test_value.value() : " + annotation_test_value.value());

        }catch(Exception e) {

            e.printStackTrace();

        }

        try {

            Thread.sleep(100);

            //注掉B中对f2的覆盖,getDeclaredMethod可以找到f2方法,也可以获取继承的注解信息

            //保留B中对f2的覆盖,getDeclaredMethod可以找到f2方法,但获取不到继承的注解信息

            Test_Value annotation_test_value =  B.class.getDeclaredMethod("f2").getAnnotation(Test_Value.class);

            System.out.println("getDeclaredMethod--annotation_test_value.value() : " + annotation_test_value.value());

            

        }catch(Exception e) {

            e.printStackTrace();

        }

    }

 

}

@Test_Value("class")

class A{

   @Test_Value("f1")

   public void f1() {

       

   }

   @Test_Value("f2")

   public void f2() {

       

   }

}

 

class B extends A{

    @Override

    public void f2() {}

}

你可能感兴趣的:(java)