Java 注解 应用
Java 注解是什么,在这里只写 我自己的理解
注解是像钩子一样,写在代码中,可用反射处理我们写的代码里的注解 信息。比如可以字段验证,结合AOP处理统一处理权限等操作。Spring MVC中就用到了很多注解。
元注解:指定义注解的注解。
包括@Retention @Target @Document @Inherited四种。
1.@Retention: 定义注解的保留策略
@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含; 检查性的操作,比如 @Override 和 @SuppressWarnings
@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,编译时进行一些预处理操作,比如生成辅助代码
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到 运行时去动态获取注解信息;
生命周期长度 SOURCE < CLASS < RUNTIME 。
2.@Target:定义注解的作用目标
@Target(ElementType.TYPE) //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
3.@Document:说明该注解将被包含在javadoc中
4.@Inherited:说明子类可以继承父类中的该注解
注解定义
package main.java.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author jiangqiang * @version 2.0.1 * @date 2018/4/26 **/ @Target(ElementType.TYPE)//Class, interface (including annotation type), or enum declaration @Retention(RetentionPolicy.RUNTIME) public @interface ClassAnnotation { String author() default "jiangqiang"; String version() default "1.0"; String description() default ""; }
package main.java.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author jiangqiang * @version 2.0.1 * @date 2018/4/25 **/ @Target(ElementType.FIELD)//Field declaration (includes enum constants) @Retention(RetentionPolicy.RUNTIME) public @interface FieldAnnotation { int length() default 0; String description() default ""; }
package main.java.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * @author jiangqiang * @version 2.0.1 * @date 2018/4/26 **/ @Target(ElementType.METHOD)//Method declaration @Retention(RetentionPolicy.RUNTIME) public @interface MethodAnnotation { String author() default "jianqiang"; String version() default "1.0"; String description() default ""; }
注解使用
package main.java.example; import main.java.annotation.ClassAnnotation; import main.java.annotation.MethodAnnotation; /** * @author jiangqiang * @version 2.0.1 * @date 2018/4/26 **/ @ClassAnnotation(author = "demo",version = "10.2",description = "测试注解类") public class AnnotationExample { @MethodAnnotation(author = "jiangqiang",version = "1.3",description = "测试注解方法") public void AnnotationMethod(Entity entity,String content){ System.out.println("name:"+entity.getName()+";description:"+entity.getDescription()); System.out.println("content:"+content); } }
package main.java.example; import main.java.annotation.FieldAnnotation; /** * @author jiangqiang * @version 2.0.1 * @date 2018/4/25 **/ public class Entity { @FieldAnnotation(length = 2,description = "名称") private String name; @FieldAnnotation(length = 5,description = "描述") private String description; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
注解应用
import main.java.annotation.FieldAnnotation; import java.lang.reflect.Field; import java.lang.reflect.Method; /** * Hello world! * */ public class App { public static void main( String[] args ) throws Exception { //类注解 Class<AnnotationExample> annotationExampleClass = AnnotationExample.class; ClassAnnotation classAnnotation = annotationExampleClass.getAnnotation(ClassAnnotation.class); System.out.println(classAnnotation.author()); System.out.println(classAnnotation.version()); System.out.println(classAnnotation.description()); //方法注解 Method method = annotationExampleClass.getMethod("AnnotationMethod",new Class[]{Entity.class,String.class}); MethodAnnotation methodAnnotation = method.getAnnotation(MethodAnnotation.class); System.out.println(methodAnnotation.author()); System.out.println(methodAnnotation.version()); System.out.println(methodAnnotation.description()); //字段注解 Class<Entity> entityClass= Entity.class; Field[] fields = entityClass.getDeclaredFields(); for(Field field:fields){ FieldAnnotation fieldAnnotation = field.getAnnotation(FieldAnnotation.class); System.out.println(fieldAnnotation.length()); System.out.println(fieldAnnotation.description()); } //根据字段名获取 Field field = entityClass.getDeclaredField("name"); FieldAnnotation entityField = field.getAnnotation(FieldAnnotation.class); System.out.println(entityField.length()); System.out.println(entityField.description()); } }