zoukankan      html  css  js  c++  java
  • java注解(Annotation)

    Retention型态可以在您定义Annotation型态时,指示编译程序该如何对待您的自定义的Annotation型态。预设上编译程序会将Annotation信息留在.class档案中,但不被虚拟机读取,而仅仅用于编译程序或工具程序运行时提供信息。


    在使用Retention时必须要提供一个RetentionPolicy的枚举类型参数。

    RetentionPolicy有三个枚举内容:CLASS RUNTIME SOURCE
         SOURCE, //编译程序处理完Annotation信息后就完成任务
         CLASS,  //编译程序将Annotation储存于class档中,缺省
         RUNTIME //编译程序将Annotation储存于class檔中,可由VM读入(通过反射机制)。这个功能搭配反射是非常强大的。

    当使用@Retention时要记得加参数和加入"包"。

    例子:

       第一步:新建一个annotation,名字为:MyAnnotation.java。注意引入了Retention和RetentionPolicy这两个包

    Java代码 复制代码 收藏代码
    1. package test;   
    2.   
    3. import java.lang.annotation.Retention;   
    4. import java.lang.annotation.RetentionPolicy;   
    5.   
    6. @Retention(RetentionPolicy.RUNTIME)   
    7. public @interface MyAnnotation {   
    8.     String hello() default "sunqitang";   
    9.     String world();   
    10. }  
    package test;
    
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    
    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation {
    	String hello() default "sunqitang";
    	String world();
    }
    

       第二步:建立一个MyTest.java 来使用上面的annotation。

    Java代码 复制代码 收藏代码
    1. public class MyTest {   
    2.     @MyAnnotation(world="Hello,world!",hello="Hello,beijing!")   
    3.     public void output(){   
    4.         System.out.println("output is running");   
    5.     }   
    6. }  
    public class MyTest {
    	@MyAnnotation(world="Hello,world!",hello="Hello,beijing!")
    	public void output(){
    		System.out.println("output is running");
    	}
    }

      第三步:用反射机制来调用注解中的内容

    Java代码 复制代码 收藏代码
    1. import java.lang.annotation.Annotation;   
    2. import java.lang.reflect.Method;   
    3.   
    4. public class MyReflection  {   
    5.     public static void main(String[] args) throws Exception {   
    6.         Class<MyTest> c = MyTest.class;//获得要调用的类   
    7.         Method method = c.getMethod("output", new Class[]{});//获得要调用的方法,output为要调用方法名字,new Class[]{}为所需要的参数。由于为空,所以使用这个方式   
    8.         if(method.isAnnotationPresent(MyAnnotation.class)){//是否有类型为MyAnnotation的注解   
    9.             MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);//获得注解   
    10.             System.out.println(annotation.hello()); //调用注解内容   
    11.             System.out.println(annotation.world());//调用注解内容   
    12.         }   
    13.         System.out.println("------------------------");   
    14.         Annotation[] annotations = method.getAnnotations();//获得所有注解。必须是在runtime类型的。   
    15.         for(Annotation annotation: annotations){//遍历所以注解的名称   
    16.             System.out.println(annotation.annotationType().getName());   
    17.         }   
    18.     }   
    19. }  
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Method;
    
    public class MyReflection  {
    	public static void main(String[] args) throws Exception {
    		Class<MyTest> c = MyTest.class;//获得要调用的类
    		Method method = c.getMethod("output", new Class[]{});//获得要调用的方法,output为要调用方法名字,new Class[]{}为所需要的参数。由于为空,所以使用这个方式
    		if(method.isAnnotationPresent(MyAnnotation.class)){//是否有类型为MyAnnotation的注解
    			MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);//获得注解
    			System.out.println(annotation.hello()); //调用注解内容
    			System.out.println(annotation.world());//调用注解内容
    		}
    		System.out.println("------------------------");
    		Annotation[] annotations = method.getAnnotations();//获得所有注解。必须是在runtime类型的。
    		for(Annotation annotation: annotations){//遍历所以注解的名称
    			System.out.println(annotation.annotationType().getName());
    		}
    	}
    }
    

     运行结果:

    运行结果代码 复制代码 收藏代码
    1. Hello,beijing!   
    2. Hello,world!   
    3. ------------------------   
    4. test.MyAnnotation 
  • 相关阅读:
    spring容器启动
    springmvc流程
    bean作用域
    bean的生命周期
    web.xml详解
    设计模式 工厂和抽象工厂
    requests 模块
    爬虫基础
    提高级
    循环语句
  • 原文地址:https://www.cnblogs.com/shijiaoyun/p/4213261.html
Copyright © 2011-2022 走看看