JAVA注解说明及应用实例
来源:互联网 发布:小米笔记本装mac系统 编辑:程序博客网 时间:2024/05/21 17:19
前言
特别基础的东西不啰嗦了, 概念之类的, 百度一下一大堆
1.元注解
元注解是指注解的注解。包括 @Retention
@Target
@Document
@Inherited
四种。
1.1 @Target
表示该注解目标,可能的 ElemenetType 参数包括:
ElemenetType.CONSTRUCTOR 构造器声明ElemenetType.FIELD 域声明(包括 enum 实例) ElemenetType.LOCAL_VARIABLE 局部变量声明 ElemenetType.METHOD 方法声明 ElemenetType.PACKAGE 包声明 ElemenetType.PARAMETER 参数声明 ElemenetType.TYPE 类,接口(包括注解类型)或enum声明@Target({ElementType.TYPE, ElementType.METHOD}) 定义多个
1.2 @Retention
表示该注解的生命周期,可选的 RetentionPolicy 参数包括
RetentionPolicy.SOURCE 注解将被编译器丢弃 RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃 RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息
1.3 @Documented
指示将此注解包含在 javadoc 中
1.4 @Inherited
指示允许子类继承父类中的注解
2.自定义注解
首先, 自定义注解可以指定如上4个元注解, 指定目标 (类、方法、字段, 构造函数等) , 注解的生命周期(运行时,class文件或者源码中有效), 是否将注解包含在javadoc中及是否允许子类继承父类中的注解.
其他注意事项:
1. Annotation型定义为@interface, 所有的Annotation会自动继承java.lang.Annotation这一接口,并且不能再去继承别的类或是接口.2. 参数成员只能用public或默认(default)这两个访问权修饰3. 参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和String、Enum、Class、annotations等数据类型,以及这一些类型的数组.4. 要获取类方法和字段的注解信息,必须通过Java的反射技术来获取 Annotation对象,因为你除此之外没有别的获取注解对象的方法5. 注解也可以没有定义成员, 不过这样注解就没啥用了
定义注解格式: public @interface 注解名 {定义体}
一组自定义注解:
public class MyAnno { /* * 类注解 */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface ClassAnno { String desc(); String value(); } /* * 构造注解 */ @Target(ElementType.CONSTRUCTOR) @Retention(RetentionPolicy.RUNTIME) public @interface ConstructorAnno { String desc(); String value(); } /* * 方法注解 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MethodAnno { String desc(); String value(); } /* * 字段注解 */ @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface FiledAnno { String desc(); String value(); } /* * 可以同时应用到类上和方法上 */ @Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface Yts { // 定义枚举 public enum YtsType { util, entity, service, model } // 设置默认值 public YtsType classType() default YtsType.util; // 数组 int[] arr() default {3, 7, 5}; String color() default "blue"; }}
测试注解:
@ClassAnno(value="class",desc="类注解")public class AnnoTest { @ConstructorAnno(desc="构造注解",value="constructor") public AnnoTest() { } @MethodAnno(desc="方法注解", value="method") public static void methodAnnoTest() { } @FiledAnno(desc="字段注解", value="filed") private String filedAnnoTest; public static void main(String[] args) throws NoSuchMethodException, SecurityException, NoSuchFieldException { Class<AnnoTest> clazz = AnnoTest.class; ClassAnno annotation = clazz.getAnnotation(ClassAnno.class); System.out.println(annotation.desc() + " " + annotation.value()); Constructor<AnnoTest> constructor = clazz.getConstructor(); ConstructorAnno annotation2 = constructor.getAnnotation(ConstructorAnno.class); System.out.println(annotation2.desc() + " " + annotation2.value()); Method method = clazz.getMethod("methodAnnoTest", null); MethodAnno annotation3 = method.getAnnotation(MethodAnno.class); System.out.println(annotation3.desc() + " " + annotation3.value()); Field field = clazz.getDeclaredField("filedAnnoTest"); FiledAnno annotation4 = field.getAnnotation(FiledAnno.class); System.out.println(annotation4.desc() + " " + annotation4.value()); }}
枚举类型用法:
@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)public @interface FruitColor { public enum Color {RED,YELLOW,GREEN}; public Color value() default Color.RED;}public class Apple { @FruitColor(value = Color.YELLOW) private String color;}
3.注解元素的默认值
注解元素必须有确定的值,要么在定义注解的默认值中指定,要么在使用注解时指定,非基本类型的注解元素的值不可为null。因此, 使用空字符串或0作为默认值是一种常用的做法。这个约束使得处理器很难表现一个元素的存在或缺失的状态,因为每个注解的声明中,所有元素都存在,并且都具有相应的值,为了绕开这个约束,我们只能定义一些特殊的值,例如空字符串或者负数,一次表示某个元素不存在,在定义注解时,这已经成为一个习惯用法。例如:
package annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 水果供应者注解 * @author peida * */@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)@Documentedpublic @interface FruitProvider { /** * 供应商编号 * @return */ public int id() default -1; /** * 供应商名称 * @return */ public String name() default ""; /** * 供应商地址 * @return */ public String address() default "";}
注解实现拦截权限管理
实现发送一个请求, 被拦截器拦截验证是否具有使用权限的功能, 定义个一个权限管理注解:
@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface Premission { // 角色 public String role(); // 权限 public String privilege();}
Controller部分代码:
// role必须是"卖家" 并且具有"导出"权限才可访问该请求@Premission(role = "seller", privilege = "export")public ModelAndView exportExcel(HttpServletRequest request, HttpServletResponse response) { // 导出Excel操作... return new ModelAndView("somewhere");}
拦截处理器类:
HandlerInterceptorpublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String servletPath = request.getServletPath(); int lastIndexOf = servletPath.lastIndexOf("."); int startIndexOf = servletPath.lastIndexOf("/"); String substring = servletPath.substring(startIndexOf + 1, lastIndexOf); // spring3.0以后可以直接(HandlerMethod) method = (HandlerMethod) handler; // 核心就是要获得这个方法对象 Method declaredMethod = handler.getClass().getDeclaredMethod(substring, HttpServletRequest.class, HttpServletResponse.class); Premission premission = declaredMethod.getAnnotation(Premission.class); String role = request.getParameter("role"); if (role != null && role.equals(premission.role())) { return true; } request.getRequestDispatcher(request.getContextPath() + "/shop/wares/initSave.jhtml").forward(request, response); return false; } // 另外: 用这个api也可以 表示是否有注解 // boolean annotationPresent = //declaredMethod.isAnnotationPresent(Premission.class);
阅读全文
0 0
- JAVA注解说明及应用实例
- java注解应用实例
- java注解应用实例
- java 自定义注解应用实例
- java注解应用实例 - Annotation, 自定义注解, 注解类规则
- Java注解应用实例 - Annotation, 自定义注解, 注解类规则
- java注解应用实例 - Annotation, 自定义注解, 注解类规则
- java注解应用实例 - Annotation, 自定义注解, 注解类规则
- java注解应用实例 - Annotation, 自定义注解, 注解类规则
- java注解应用实例 - Annotation, 自定义注解, 注解类规则
- Java反射实例及说明
- Ajax简要应用说明及技术开发实例
- Ajax简要应用说明及技术开发实例
- Ajax简要应用说明及技术开发实例
- java注解(2)之应用实例
- spring MVC配置说明及Controller注解实例
- Java模块 -- Java注解及应用
- 存储过程应用实例及应用语法说明
- 洛谷 P1074 靶形数独
- os_time.c
- 前端性能优化之视频优化方法
- QML中鼠标拖动移动ListView中项的位置
- Ubuntu16.04自带Python2.7和Python3,为什么用pip安装的包在Python3中不能用
- JAVA注解说明及应用实例
- 【leetcode】20. Valid Parentheses(Python & C++)
- BMP格式-dephi
- Nginx+Tomcat搭建负载均衡
- 天地图按照官方教程闪退的问题
- jQuery选择器
- @ModelAttribute
- arcgis rest server动态图层管理
- java中的反射机制