java annotation 入门alibaba - 牛牛娱乐

java annotation 入门alibaba

2018-10-04 08:51:04 | 作者: 傲柔 | 标签: 信息,类型,办法 | 浏览: 8194

一. 最常见的annotation
@Override:用在办法之上,用来通知他人这一个办法是改写父类的 
@DePRecated:主张他人不要使用旧的API的时分用的,编译的时分会用发生正告信息,能够设定在程序里的一切的元素上. 
@SuppressWarnings:暂时把一些正告信息音讯封闭 
@Entity:表明该类是可耐久化的类 

二. 规划一个自己的Annotation
      先看代码再说话
1. 只要一个参数的Annotation完成

view plaincopy to clipboardprint?
package chb.test.annotation;    
import java.lang.annotation.Documented;    
import java.lang.annotation.ElementType;    
import java.lang.annotation.Retention;    
import java.lang.annotation.RetentionPolicy;    
import java.lang.annotation.Target;    
@Target(ElementType.TYPE)    
@Retention(RetentionPolicy.RUNTIME)    
@Documented   
public @interface MyAnnotation1 {    
        String value();    
}   
view plaincopy to clipboardprint?
package chb.test.annotation;   
import java.lang.annotation.Documented;   
import java.lang.annotation.ElementType;   
import java.lang.annotation.Retention;   
import java.lang.annotation.RetentionPolicy;   
import java.lang.annotation.Target;   
@Target(ElementType.TYPE)   
@Retention(RetentionPolicy.RUNTIME)   
@Documented  
public @interface MyAnnotation1 {   
        String value();   
}  
package chb.test.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation1 {
        String value();


2. 有两个参数的Annotation完成

view plaincopy to clipboardprint?
package chb.test.annotation;    
import java.lang.annotation.Documented;    
import java.lang.annotation.ElementType;    
import java.lang.annotation.Retention;    
import java.lang.annotation.RetentionPolicy;    
import java.lang.annotation.Target;    
@Target(ElementType.METHOD)    
@Retention(RetentionPolicy.RUNTIME)    
@Documented   
public @interface MyAnnotation2 {    
        String description();    
        boolean isAnnotation();    
}   
view plaincopy to clipboardprint?
package chb.test.annotation;   
import java.lang.annotation.Documented;   
import java.lang.annotation.ElementType;   
import java.lang.annotation.Retention;   
import java.lang.annotation.RetentionPolicy;   
import java.lang.annotation.Target;   
@Target(ElementType.METHOD)   
@Retention(RetentionPolicy.RUNTIME)   
@Documented  
public @interface MyAnnotation2 {   
        String description();   
        boolean isAnnotation();   
}  
package chb.test.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation2 {
        String description();
        boolean isAnnotation();


3. Annotation试验类

view plaincopy to clipboardprint?
package chb.test.annotation;    
@MyAnnotation1("this is annotation1")    
public class AnnotationDemo {    
        @MyAnnotation2(description="this is annotation2",isAnnotation=true)    
        public void sayHello(){    
                System.out.println("hello world!");    
        }    
}   
view plaincopy to clipboardprint?
package chb.test.annotation;   
@MyAnnotation1("this is annotation1")   
public class AnnotationDemo {   
        @MyAnnotation2(description="this is annotation2",isAnnotation=true)   
        public void sayHello(){   
                System.out.println("hello world!");   
        }   
}  
package chb.test.annotation;
@MyAnnotation1("this is annotation1")
public class AnnotationDemo {
        @MyAnnotation2(description="this is annotation2",isAnnotation=true)
        public void sayHello(){
                System.out.println("hello world!");
        }


4.Annotation测验阐明类

view plaincopy to clipboardprint?
package chb.test.annotation;    
import java.lang.reflect.Method;    
import org.junit.Test;    
public class TestAnnotation {    
        @Test   
        public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException{    
                Class ? cls = Class.forName("chb.test.annotation.AnnotationDemo");    
                boolean flag = cls.isAnnotationPresent(MyAnnotation1.class);    
                if(flag){    
                        System.out.println("判别类是annotation");    
                        MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class);    
                        System.out.println(annotation1.value());    
                }    
                    
                Method method = cls.getMethod("sayHello");    
                flag = method.isAnnotationPresent(MyAnnotation2.class) ;    
                if(flag){    
                        System.out.println("判别办法也是annotation");    
                        MyAnnotation2 annotation2 = method.getAnnotation(MyAnnotation2.class);    
                        System.out.println(annotation2.description()+"\t"+annotation2.isAnnotation());    
                }    
        }    
            
}   
view plaincopy to clipboardprint?
package chb.test.annotation;   
import java.lang.reflect.Method;   
import org.junit.Test;   
public class TestAnnotation {   
        @Test  
        public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException{   
                Class ? cls = Class.forName("chb.test.annotation.AnnotationDemo");   
                boolean flag = cls.isAnnotationPresent(MyAnnotation1.class);   
                if(flag){   
                        System.out.println("判别类是annotation");   
                        MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class);   
                        System.out.println(annotation1.value());   
                }   
                   
                Method method = cls.getMethod("sayHello");   
                flag = method.isAnnotationPresent(MyAnnotation2.class) ;   
                if(flag){   
                        System.out.println("判别办法也是annotation");   
                        MyAnnotation2 annotation2 = method.getAnnotation(MyAnnotation2.class);   
                        System.out.println(annotation2.description()+"\t"+annotation2.isAnnotation());   
                }   
        }   
           
}  
package chb.test.annotation;
import java.lang.reflect.Method;
import org.junit.Test;
public class TestAnnotation {
        @Test
        public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException{
                Class ? cls = Class.forName("chb.test.annotation.AnnotationDemo");
                boolean flag = cls.isAnnotationPresent(MyAnnotation1.class);
                if(flag){
                        System.out.println("判别类是annotation");
                        MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class);
                        System.out.println(annotation1.value());
                }
                
                Method method = cls.getMethod("sayHello");
                flag = method.isAnnotationPresent(MyAnnotation2.class) ;
                if(flag){
                        System.out.println("判别办法也是annotation");
                        MyAnnotation2 annotation2 = method.getAnnotation(MyAnnotation2.class);
                        System.out.println(annotation2.description()+"\t"+annotation2.isAnnotation());
                }
        }
        


试验成果,控制台打出如下信息:

判别类是annotation
this is annotation1
判别办法也是annotation
this is annotation2     true

三.简介及阐明

1. MyAnnotation1中的@Target(ElementType.TYPE)
      @Target里边的ElementType是用来指定Annotation类型能够用在哪些元素上的.例如:
       TYPE(类型)、FIELD(特点)、METHOD(办法)、PARAMETER(参数)、CONSTRUCTOR(结构函数)、LOCAL_VARIABLE(局部变量),、PACKAGE(包),其间的TYPE(类型)是指能够用在Class,Interface,Enum和Annotation类型上。
2. MyAnnotation1中的@Retention(RetentionPolicy.RUNTIME)
      RetentionPolicy 共有三种战略,分别为:
SOURCE:这个Annotation类型的信息只会保存在程序源码里,源码假如经过了编译之后,Annotation的数据就会消失,并不会保存在编译好的.class文件里边 
CLASS:这个Annotation类型的信息保存在程序源码里,一起也会保存在编译好的.class文件里边,在履行的时分,并不会把这些信息加载到JVM中。注:默许战略为CLASS类型 
RUNTIME:表明在源码、编译好的.class文件中保存信息,在履行的时分会把这一些信息加载到JVM中去的 
3. MyAnnotation1中的@Documented
意图就是将这一Annotation的信息显现在JAVA API文档上,假如没有添加@Documented的话,JAVA API文档上不会显现相关annotation信息

4. MyAnnotation1中的@interface
   关键字,表明该类为Annotation界说
5. MyAnnotation1中的 String value();
   表明有一个成员参数,姓名为value,拜访权为默许(default)润饰符,留意以下两点:
拜访权只能用public和默许(default)润饰 
参数成员只能用根本类型byte,short,char,int,long,float,double,boolean八种根本数据类型和String,Enum,Class,annotations等数据类型,以及这一些类型的数组 
6.AnnotationDemo中的@MyAnnotation1("this is annotation1")
   由于MyAnnotation1只要一个参数,因而能够直接在括号中写上value值。注:假如Annotation只要一个参数,则主张最好将该参数称号界说为value
7.TestAnnotation中的cls.isAnnotationPresent(MyAnnotation1.class)
    判别该类是否使用了MyAnnotation1的注释
8. TestAnnotation中的MyAnnotation1 annotation1 = cls.getAnnotation(MyAnnotation1.class)
    回来该类针对MyAnnotation1的注释
9. TestAnnotation中的method.isAnnotationPresent(MyAnnotation2.class) 
    判别该办法是否使用了MyAnnotation2的注释

版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表牛牛娱乐立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章