欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

反射与Annotation

程序员文章站 2024-01-20 17:00:46
...

Annotation使用前提:需要存在代码容器,才可以实现自定义的Annotation.

Annotation注解可以定义在类或方法上,在学习反射之后,我们可以通过反射取得定义的Annotation信息.

1.反射取得Annotation的信息:

1.取得全部Annotation:public Annotation[] getAnnotations();

2.取得指定的Annotation:public T getAnnotation(Class annotationClass);

对应举例:

import java.io.Serializable;
import java.lang.annotation.Annotation;

@SuppressWarnings("serail")
@Deprecated
class Member implements Serializable{}
public class Test12{
    public static void main(String[] args){
        Annotation[] ant = Member.class.getAnnotations();//取得全部信息
        for (Annotation i:ant){
            System.out.println(i);
        }
    }
}

在方法上使用Annotation:

import java.io.Serializable;
import java.lang.annotation.Annotation;

@SuppressWarnings("serail")
@Deprecated
class Member implements Serializable{
    @Deprecated
    @Override
    public String toString() {
        return super.toString();
    }
}
public class Test12{
    public static void main(String[] args){
        Annotation[] ant = new Annotation[0];
        try {
            ant = Member.class.getMethod("toString").getAnnotations();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        for (Annotation i:ant){
            System.out.println(i);
        }
    }
}

 由上可知:Annotation本身存在自身的保存范围,不同的Annotation返回不同.

2.自定义Annotation:
若想自定义Annotation,首先需要解决Annotation的作用范围. 

1.SOURCE:在源代码中出现的Annotation

2.CLASS:在*.class中出现的Annotation

3.RUNTIME:在类执行的时候出现的Annotation

定义一个在运行时生效的Annotation:

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    public String name();
    public int age();
}

@Deprecated
@MyAnnotation(name = "lala",age = 20)//强制设置属性值
class Member implements Serializable{

}
public class Test12{
    public static void main(String[] args){
        Annotation[] ant = new Annotation[0];
        ant = Member.class.getAnnotations();
        for (Annotation i:ant){
            System.out.println(i);
        }
    }
}

默认值定义:

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    public String name() default "lala";
    public int age() default 20;
}

class Member implements Serializable{

}
public class Test12{
    public static void main(String[] args){
        Annotation[] ant = new Annotation[0];
        ant = Member.class.getAnnotations();
        for (Annotation i:ant){
            System.out.println(i);
        }
    }
}

取得具体信息:

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    public String name() default "lala";
    public int age() default 20;
}
@Deprecated
@MyAnnotation
class Member implements Serializable{

}
public class Test12{
    public static void main(String[] args){
        MyAnnotation myAnnotation = Member.class.getAnnotation(MyAnnotation.class);
        System.out.println("name:"+myAnnotation.name());
        System.out.println("age:"+myAnnotation.age());
    }
}

注:Annotation使用需要特殊环境,不是随意编写的.

3.Annotation与工厂设计模式

对应举例:

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    public Class<?> target();
}
interface Ifruit{
    public void eat();
}
class Apple implements Ifruit{
    public void eat() {
        System.out.println("吃苹果");
    }
}
@MyAnnotation(target = Apple.class)
class Factory{
    public static <T> T getInstance() throws Exception{
        MyAnnotation myAnnotation = Factory.class.getAnnotation(MyAnnotation.class);
        return (T) myAnnotation.target().newInstance();
    }
}
public class Test12{
    public static void main(String[] args) throws Exception {
        Ifruit ifruit = Factory.getInstance();
        ifruit.eat();
    }
}

注:Annotation可以很好地解决程序配置问题.程序可通过Annotation配置信息来实现不同的操作效果.