java自定义注解Annotation
注解,简单的说就是给这个东西定义的一个标签,eg entity=一个人,那么注解可以是 土豪,穷人 等等。
下面来说说java常见的内置注解:
@Override 当我们想重写一个方法时,在方法上加@Override,当我们方法的名字出错时,编译器就会报错。 定义如下:
@Retention(RetentionPolicy.SOURCE )
public @interface Override
@Deprecated 用来表示某个类的属性或方法已经过时,不想别人再用时,在属性和方法上用@Deprecated修饰。 定义如下:
@Retention(RetentionPolicy.SOURCE )
public @interface Deprecated
@SuppressWarnings 用来压制程序中出来的警告。 定义如下:
@Retention(RetentionPolicy.SOURCE )
public @interface SuppressWarnings
自定义注解:
package com.test.testdemo.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @program: Test
* @description:
* @author: yddacy
* @create: 2018-12-06 09:28
**/
/**
* @Retention :用来说明该注解类的生命周期。它有以下三个参数:
*
* RetentionPolicy.SOURCE : 注解只保留在源文件中
* RetentionPolicy.CLASS : 注解保留在class文件中,在加载到JVM虚拟机时丢弃
* RetentionPolicy.RUNTIME : 注解保留在程序运行期间,此时可以通过反射获得定义在某个类上的所有注解。
*/
@Retention(RetentionPolicy.RUNTIME)
/**
* @Target : 用来说明该注解可以被声明在那些元素之前。
*
* ElementType.TYPE:说明该注解只能被声明在一个类前。
* ElementType.FIELD:说明该注解只能被声明在一个类的字段前。
*ElementType.METHOD:说明该注解只能被声明在一个类的方法前。
*ElementType.PARAMETER:说明该注解只能被声明在一个方法参数前。
*ElementType.CONSTRUCTOR:说明该注解只能声明在一个类的构造方法前。
*ElementType.LOCAL_VARIABLE:说明该注解只能声明在一个局部变量前。
*ElementType.ANNOTATION_TYPE:说明该注解只能声明在一个注解类型前。
*ElementType.PACKAGE:说明该注解只能声明在一个包名前。
*/
@Target(ElementType.FIELD)
public @interface TestAnnotation {
int id() default -1;
String value() default "";
}
1.元注解———5种:分别为:@Retention、@Documented、@Target、@Inherited、@Repeatable
@Retention
这个注解是表名自定义注解的存活时间,它有以下3个参数
RetentionPolicy.SOURCE : 注解只保留在源文件中。
RetentionPolicy.CLASS : 注解保留在class文件中,在加载到JVM虚拟机时丢弃。
RetentionPolicy.RUNTIME : 注解保留在程序运行期间,此时可以通过反射获得定义在某个类上的所有注解。
@Documented
一个简单的Annotations标记注解,表示是否将注解信息添加在java文档中
@Target
表示该注解用于什么地方。如果不明确指出,该注解可以放在任何地方。以下是一些可用的参数。需要说明的是:属性的注解是兼容的,如果你想给7个属性都添加注解,仅仅排除一个属性,那么你需要在定义target包含所有的属性。
ElementType.TYPE:说明该注解只能被声明在一个类前。
ElementType.FIELD:说明该注解只能被声明在一个类的字段前。
ElementType.METHOD:说明该注解只能被声明在一个类的方法前。
ElementType.PARAMETER:说明该注解只能被声明在一个方法参数前。
ElementType.CONSTRUCTOR:说明该注解只能声明在一个类的构造方法前。
ElementType.LOCAL_VARIABLE:说明该注解只能声明在一个局部变量前。
ElementType.ANNOTATION_TYPE:说明该注解只能声明在一个注解类型前。
ElementType.PACKAGE:说明该注解只能声明在一个包名前。
@Inherited
自定义注解加上这个元注解,然后自定义注解再去注解一个父类,如果有子类继承父类,那么这个子类也同样会继承父类的自定义注解。
@Repeatable
看下面代码@Repeatable 注解了 Person。而 @Repeatable 后面括号中的类相当于一个容器注解。里面的属性可以相当于一个数组看待,这个元注解是在jdk1.8才有的
@Repeatable(Persons.class)
public @interface Person{
String role() default "";
}
@Person(role="CEO")
@Person(role="husband")
@Person(role="father")
@Person(role="son")
public class Man {
String name="";
}
注解的使用:
1.先定义一个注解:
package com.test.testdemo.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
public @interface TestAnnotation {
int id() default -1;
String value() default "";
}
2.定义一个实体类,加上注解:
package com.test.testdemo.model;
import com.test.testdemo.annotation.TestAnnotation;
public class TestModel {
@TestAnnotation(value = "张三")
private String name;
@TestAnnotation(id = 18)
private String age;
@TestAnnotation(value = "三年级")
private String grade;
@TestAnnotation(id = 88)
private String score;
}
3.在main方法里面测试,利用反射获取注解的值
public static void main(String[] args){
Class clazz = TestModel.class;
Field[] cField = clazz.getDeclaredFields();
for (Field field : cField) {
TestAnnotation fruitName = field.getAnnotation(TestAnnotation.class);
if (fruitName != null) {
System.out.println(fruitName.value());
}
}
}
上面main方法运行的结果:
张三
三年级
好了,现在注解的使用就完成了
还没有评论,来说两句吧...