注解使用的场景:
做过web开发的都知道,很多开发框架都使用注解,比如Spring中如下代码:
@RequestMapping(value="/inputPerson")
public String inputPerson(){
System.out.println("..........inputPerson");
return "inputPerson";
}
@RequestMapping(value="/updatePerson")
public String updatePerson(){
System.out.println("..........updatePerson");
return "updatePerson";
}
这段代码中 RequestMapping就是一个注解,注解前面用@符号修饰,这个注解作用于方法上,里面的value用于注解赋值。
注解类型:
1:标准注解:
Override作用:保证编译时候Override函数的申明正确性。
Deprecated作用:保证编译时候Override函数的申明正确性。
SuppressWarnings 关闭特定警告,他的主要参数有:
1):deprecation:使用了过时的类或者方法时的警告
2):unchecked 执行了未检查的转换时的警告
3):path 在类路径,源文件路径等有不存在的路径是的警告
4):serial 当在可序列话的类缺少
5):serialVersionUID定义时的警告
6):finally 任何finally子句不能正常完成时的警告
7):all 关于以上所有情况的警告
元注解:(负责注解其他注解)
@Retention (主要参数:SOURCE CLASS RUNTIME)
@Target (主要参数:CONSTRUCTOR,FIELD,LOCAL_VARIABLE,METHOD,PACKAGE,PARAMETER,TYPE)
@Documented (jiang 将注释包含在Javadoc中)
@Inhertied(允许子类继承父类中的注释)
看以下例子:
我们定义一个注解,这个注解作用于字段所有Target的value值是ElementType.FIELD
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FiledAnnotation {
String userName() default "zhangming";
String passWord() default "123456";
String unit() default "android";
}
定义Person使用注解,并且通过注解给Person对象的字段赋值:
public class Person {
@FiledAnnotation(userName="小明")
public String userName;
@FiledAnnotation(passWord="abc123456")
public String passWord;
@FiledAnnotation(unit="java")
public String unit;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
@Override
public String toString() {
return "Person [userName=" + userName + ", passWord=" + passWord + ", unit=" + unit + "]";
}
}
测试结果查看注解是否运行正确:
public static void main(String[] args) {
//创建一个Person对象
Person person = new Person();
//获取person对象对应的字段数组
Field[] fields = person.getClass().getFields();
for(int i = 0;i < fields.length;i++){
//判断当前是否是userName字段,并且userName字段中的的注解是否是FiledAnnotation
if(fields[i].isAnnotationPresent(FiledAnnotation.class) && fields[i].getName().equals("userName")){
FiledAnnotation filedAnnotation = fields[i].getAnnotation(FiledAnnotation.class);
System.out.println(filedAnnotation.userName());
//判断当前是否是passWord字段,并且userName字段中的的注解是否是FiledAnnotation
}else if(fields[i].isAnnotationPresent(FiledAnnotation.class) && fields[i].getName().equals("passWord")){
FiledAnnotation filedAnnotation = fields[i].getAnnotation(FiledAnnotation.class);
System.out.println(filedAnnotation.passWord());
//判断当前是否是unit字段,并且userName字段中的的注解是否是FiledAnnotation
}else if(fields[i].isAnnotationPresent(FiledAnnotation.class) && fields[i].getName().equals("unit")){
FiledAnnotation filedAnnotation = fields[i].getAnnotation(FiledAnnotation.class);
System.out.println(filedAnnotation.unit());
}
}
}
通过以上例子控制台打印结果如下:
小明
abc123456
java
这里我们在Person对象中用注解赋值当前对象字段,通过反射方法得到当前对象的字段名,然后调用isAnnotationPresent判断当前字段是否包含FiledAnnotation注解,如果是,最后通过getAnnotation()方法获取FiledAnnotation实例,最后输出当前person对象注解赋予的值。