Java -- 注解 annotation

1.

JDK 5.0 开始, Java 增加了对元数据(MetaData) 的支持, 也就是 Annotation(注解)
什么是Annotation,以及注解的作用?三个基本的 Annotation:
@Override: 限定重写父类方法, 该注解只能用于方法
@Deprecated: 用于表示某个程序元素(, 方法等)已过时
@SuppressWarnings: 抑制编译器警告.
Annotation 其实就是代码里的特殊标记, 它用于替代配置文件,也就是说,传统方式通过配置文件告诉类如何运行,有了注解技术后,开发人员可以通过注解告诉类如何运行。在Java技术里注解的典型应用是:可以通过反射技术去得到类里面的注解,以决定怎么去运行类。
 
定义新的 Annotation 类型使用 @interface 关键字
声明注解的属性
注解属性的作用:原来写在配置文件中的信息,可以通过注解的属性进行描述。
Annotation 的属性声明方式:String name();
属性默认值声明方式:String name() default “xxx”;
特殊属性value:如果注解中有一个名称value的属性,那么使用注解时可以省略value=部分,如@MyAnnotation(“xxx")
特殊属性value[];
 
元 Annotation指修饰Annotation的Annotation。JDK中定义了如下元Annotation:
@Retention: 只能用于修饰一个 Annotation 定义, 用于指定该 Annotation 可以保留的域, @Rentention 包含一个 RetentionPolicy 类型的成员变量, 通过这个变量指定域。
RetentionPolicy.CLASS: 编译器将把注解记录在 class 文件中. 当运行 Java 程序时, JVM 不会保留注解. 这是默认值
RetentionPolicy.RUNTIME:编译器将把注释记录在 class 文件中. 当运行 Java 程序时, JVM 会保留注解. 程序可以通过反射获取该注释
RetentionPolicy.SOURCE: 编译器直接丢弃这种策略的注释
@Target:指定注解用于修饰类的哪个成员. @Target 包含了一个名为 value,类型为ElementType的成员变量。
@Documented: 用于指定被该元 Annotation 修饰的 Annotation 类将被 javadoc 工具提取成文档.
@Inherited: 被它修饰的 Annotation 将具有继承性.如果某个类使用了被 @Inherited 修饰的 Annotation, 则其子类将自动具有该注解.
 

Demo1

@SuppressWarnings("unchecked")
public class Demo1 {

	@SuppressWarnings("unchecked") private List list;

	@SuppressWarnings("unchecked")
	public Demo1(@SuppressWarnings("unchecked") List list) {
		super();
		this.list = list;
	}

	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		return super.equals(obj);
	}
	
	@Deprecated
	public void doxx(){
		
	}
	
	public void doyy(){
		
	}
	
	@SuppressWarnings("unchecked")
	public void dozz(){
		List list = new ArrayList();
		System.out.println(list);
	}
	
	@MyAnnotation(name="老张",age=37,gender=Gender.MALE,clazz=String.class,my2=@MyAnnotation2(name="xxx"),arr={2,3,4},gs={Gender.FEMALE,Gender.MALE})
	public void doaa(){
		
	}
	
	//名称为value且只有一个的属性可以直接赋值
	@MyAnnotation3(value={"bb"})
	public void dobb(){
		
	}
}

自定义注解 MyAnnotation

public @interface MyAnnotation {

	//注解可以使用如下类型配置注解包含的信息
	String name();
	String password() default "123";
	double age() default 12;
	Gender gender() default Gender.FEMALE;
	Class clazz();
	MyAnnotation2 my2();
	int[] arr() default {1,2,3};
	Gender[] gs();
	
}


Demo2, 先反射方法,再从注解获得参数,再执行方法

public class Demo2 {
	public static void main(String[] args) throws SecurityException, Exception {
		
		Class clazz = JdbcUtils.class;
		Method method = clazz.getMethod("getConnection", String.class,String.class,String.class);
		
		DbInfo di = method.getAnnotation(DbInfo.class);
		String url = di.url();
		String username = di.username();
		String password = di.password();
				
		method.invoke(null, url,username,password);		
	}
}

被注解的方法

public class JdbcUtils {

	@DbInfo(url="jdbc:mysql://localhost:3306/test",username="flx",password="root")
	public static Connection getConnection(String url,String username,String password){
		
		System.out.println(url);
		System.out.println(username);
		System.out.println(password);
		
		return null;
	}
	
	@DbInfo(url="jdbc:mysql://localhost:3306/test",username="flx",password="root")
	public void aa(){
		
	}	
}

自定义的注解,注意 要有元注解 @Retention

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Inherited
public @interface DbInfo {

	String url() default "jdbc:mysql://localhost:3306/test";
	String username() default "root";
	String password() default "root";
	
}





 

 
原文地址:https://www.cnblogs.com/xj626852095/p/3648028.html