注解的概念:
一般情况不会让你去开发注解,但是在以后的框架中会大量使用注解,当你看到注解的时候不要觉得它是什么神奇的东西,注解的主要作用的对代码进行跟踪,警告等等,尤其是在容器中使用广泛(在容器中就使用了反射操作注解实现某些功能,比如说Spring容器中就大量的使用了注解),还可以使用反射操作注解,看看几个Java定义出的几个注解。
@Override注解
@Override/* 表示该方法是覆父类的方法,
所以方法的形式要和父类保持一致*/
public String toString() {
return super.toString();
}
@Override源码
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
定义注解的方式:public @interface 注解名称
@Target(ElementType.METHOD):
表示注解的可以在指定的位置使用,
ElementType.METHOD 注解可以在方法上使用
@Retention(RetentionPolicy.SOURCE):
指定了注解的生命周期,SOURCE表示注解是在源码的时候生效,
当程序运行的时候就不生效了,这个效果后面我们用反射来证明
@SuppressWarnings(“serial”)注解
@SuppressWarnings("serial")
public class Emp implements Serializable {
}
该注解的本身表示压制警告。
@SuppressWarnings源码
@Target({TYPE, FIELD, METHOD, PARAMETER,CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
/**
* The set of warnings that are to be suppressed by the compiler in the
* annotated element. Duplicate names are permitted. The second and
* successive occurrences of a name are ignored. The presence of
* unrecognized warning names is <i>not</i> an error: Compilers must
* ignore any warning names they do not recognize. They are, however,
* free to emit a warning if an annotation contains an unrecognized
* warning name.
*
* <p> The string {@code "unchecked"} is used to suppress
* unchecked warnings. Compiler vendors should document the
* additional warning names they support in conjunction with this
* annotation type. They are encouraged to cooperate to ensure
* that the same names work across multiple compilers.
* @return the set of warnings to be suppressed
*/
String[] value();
}
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
表示该注解可以在:类上、属性,参数、构造方法、局部变量
@Retention(RetentionPolicy.SOURCE):注解的生命周期是在源码有效
@Deprecated注解
public void add(){
Date date=new Date();
date.getYear();
}
@Deprecated
public int getYear() {
return normalize().getYear() - 1900;
}
@Deprecated源码
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
}
@Documented:表示如果对源码进行打包成api文档,那么注解的内容也会显示到api文档中
@Retention(RetentionPolicy.RUNTIME):指定的注解的生命周期是到运行时,后面使用反射证明
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
注解的使用范围。
元注解有如下四种:
@Documented –注解是否将包含在JavaDoc中,表示是否将注解
信息添加在java文档中
@Retention–什么时候使用该注解(也就是注解的生命周期)
@Target–指定注解的只用范围
@Inherited–是否允许子类继承该注解
元注解就是用来定义注解的注解
自定义注解:
压制警告(@SuppressWarnings)、覆写(@Override)、过期声明(@Deprecated)
上面的注解是java定义好的,使用元注解定义的,我们也可以使用元注解定义自己的注解。
自定义注解
package com.sxt.annotation;
import java.lang.annotation.*;
@Target({ElementType.METHOD,ElementType.FIELD,ElementType.TYPE,ElementType.LOCAL_VARIABLE})
//指定生命周期
@Retention(RetentionPolicy.RUNTIME)
//表示可以被子类继承
@Inherited
public @interface EmpAnotaion {
String empno() default "10001";
String ename() default "smith";
}
package com.sxt;
import com.sun.annotation.EmpAnotaion;
import java.io.Serializable;
@EmpAnotaion
public class Emp implements Serializable {
@EmpAnotaion
public void add(){
}
}
使用反射读取注解:
使用反射读取注解目前来说没有什么作用,只是作为语法提出来,因为要容器中才能体现反射读取注解的优势
反射读取注解
public class Test {
public static void main(String[] args) throws Exception {
//取得堆区中的Class<T>对象
Class<?> classObj = Emp.class;
//使用Class对象操作Emp类的注解
Annotation annotation = classObj.getAnnotation(SuppressWarnings.class);
System.out.println(annotation);
//取得方法上的注解
//1、取得方法对象
Method method = classObj.getMethod("add");
//取得方法上的注解
Annotation mannotation = method.getAnnotation(EmpAnotaion.class);
System.out.println(mannotation);
}
}
压制警告的注解没有取得,原因是压制警告的注解的生命周期是在源码级别,EmpAnnotation注解的生命周期是Runtime
读取自定义注解
package com.sun;
import com.sun.annotation.EmpAnotaion;
import java.io.Serializable;
@SuppressWarnings("serial")
public class Emp implements Serializable {
@EmpAnotaion(empno = "1003")
public void add(){
}
}
public class Test {
public static void main(String[] args) throws Exception {
//取得堆区中的Class<T>对象
Class<?> classObj = Emp.class;
Method method = classObj.getMethod("add");
//取得方法上的注解
EmpAnotaion mannotation = method.getAnnotation(EmpAnotaion.class);
System.out.println("雇员的编号是:"+ mannotation.empno());
System.out.println("雇员的姓名是:"+ mannotation.ename());
}
}
如果再注解中给出了注解元素的属性的默认值,则在使用注解的时候可以不给出具体的值,如果定义了属性但是没有给出默认值,则在使用注解的时候必须给出默认值。