关于注解

JAVA 注解的几大作用及使用方法详解

 java 注解,从名字上看是注释,解释。但功能却不仅仅是注释那么简单。注解(Annotation) 为我们在代码中添加信息提供了一种形式化的方法,是我们可以在稍后 某个时刻方便地使用这些数据(通过 解析注解 来使用这些数据),常见的作用有以下几种:

1.生成文档。这是最常见的,也是java 最早提供的注解。常用的有@see @param @return 等;
2.跟踪代码依赖性,实现替代配置文件功能。比较常见的是spring 2.5 开始的基于注解配置。作用就是减少配置。现在的框架基本都使用了这种配置来减少配置文件的数量;
3.在编译时进行格式检查。如@Override放在方法前,如果你这个方法并不是覆盖了超类方法,则编译时就能检查出;
 
包 java.lang.annotation 中包含所有定义自定义注解所需用到的原注解和接口。如接口 java.lang.annotation.Annotation 是所有注解继承的接口,并且是自动继承,不需要定义时指定,类似于所有类都自动继承Object。
 
该包同时定义了四个元注解Documented,Inherited,Target(作用范围,方法,属性,构造方法等),Retention(生命范围,源代码,class,runtime)。下面将在实例中逐个讲解他们的作用,及使用方法。
 
Inherited : 在您定义注解后并使用于程序代码上时,预设上父类别中的注解并不会被继承至子类别中,您可以在定义注解时加上java.lang.annotation.Inherited 限定的Annotation,这让您定义的Annotation型别被继承下来。注意注解继承只针对class 级别注解有效(这段建议看完全文后在来回顾)。 多说无益,下面就一步步从零开始建一个我们自己的注解。

Java注解(Annotation):
  a) Override注解表示子类要重写(override)父类的对应方法。
  b) Deprecated注解表示方法是不建议被使用的。
  c) SuppressWarnings注解表示抑制警告。
3. 自定义注解:当注解中的属性名为value时,在对其赋值时可以不指定属性的名称而直接写上属性值即可;除了value以外的其他值都需要使用name=value这种赋值方式,即明确指定给谁赋值。
4. 当我们使用@interface关键字定义一个注解时,该注解隐含地继承了java.lang.annotation.Annotation接口如果我们定义了一个接口,并且让该接口继承自Annotation,那么我们所定义的接口依然还是接口而不是注解;Annotation本身是接口而不是注解可以与Enum类比
5. JUnit(3.8、4.x):Keep the bar green to keep the code clean.
6. 我的名言:没有反射,很多框架就不存在了。(No Reflection,No most frameworks)。
7. JUnit4的执行的一般流程:
  a) 首先获得待测试类所对应的Class对象。
  b) 然后通过该Class对象获得当前类中所有public方法所对应的Method数组。
  c) 遍历该Method数组,取得每一个Method对象
  d) 调用每个Method对象的isAnnotationPresent(Test.class)方法,判断该方法是否被Test注解所修饰。
  e) 如果该方法返回true,那么调用method.invoke()方法去执行该方法,否则不执行。
8. 单元测试不是为了证明你是对的,而是证明你没有错误。
9. Writing Secure Code(编写安全的代码):Input is evil。

 自定义注解

AnnotationTest.java

public @interface AnnotationTest {
    String[] value1() default "hello";
    EnumTest value2();
}

enum EnumTest {
    Hello, World, Welcome;
}

AnnotationUsage.java

@AnnotationTest(value2 = EnumTest.Welcome)
public class AnnotationUsage {
    @AnnotationTest(value1 = { "world", "ABCD" }, value2 = EnumTest.World)
    public void method() {
        System.out.println("usage of annotation");
    }

    public static void main(String[] args) {
        AnnotationUsage usage = new AnnotationUsage();

        usage.method();
    }
}

>>>>>>>>

MyAnnotation.java

@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotation {
    String hello() default "shengsiyuan";

    String world();
}

MyTest.java

@MyAnnotation(hello = "beijing", world = "shanghai")
public class MyTest {
    @MyAnnotation(hello = "tianjin", world = "shangdi")
    @Deprecated
    @SuppressWarnings("unchecked")
    public void output() {
        System.out.println("output something!");
    }
}

MyReflection.java

 1 public class MyReflection {
 2     public static void main(String[] args) throws Exception {
 3         MyTest myTest = new MyTest();
 4 
 5         Class<MyTest> c = MyTest.class;
 6 
 7         Method method = c.getMethod("output", new Class[] {});
 8 
 9         if (method.isAnnotationPresent(MyAnnotation.class)) {
10             method.invoke(myTest, new Object[] {});
11 
12             MyAnnotation myAnnotation = method
13                     .getAnnotation(MyAnnotation.class);
14 
15             String hello = myAnnotation.hello();
16             String world = myAnnotation.world();
17 
18             System.out.println(hello + ", " + world);
19         }
20 
21         Annotation[] annotations = method.getAnnotations();
22 
23         for (Annotation annotation : annotations) {
24             System.out.println(annotation.annotationType().getName());
25         }
26 
27     }
28 }

>>>>>>

MyTarget.java

@Target(ElementType.METHOD)
public @interface MyTarget {
    String value();
}

MyTargetTest.java

public class MyTargetTest {
    @MyTarget("hello")
    public void doSomething() {
        System.out.println("hello world");
    }
}

原文地址:https://www.cnblogs.com/DreamDrive/p/4217229.html