如何自定义注解及使用
2017-08-23 15:41
429 查看
1.注解介绍
注解java 5引进的新特性,是附加在代码中的一些元信息;它用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。元注解(注解的注解):
①@Retention
@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到
②@Target
@Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)方法参数和本地变量(如循环变量、catch参数),ElementType取值:
1.CONSTRUCTOR:用于描述构造器
2.FIELD:用于描述域
3.LOCAL_VARIABLE:用于描述局部变量
4.METHOD:用于描述方法
5.PACKAGE:用于描述包
6.PARAMETER:用于描述参数
7.TYPE:用于描述类、接口(包括注解类型) 或enum声明
2.实现方案
①自定义注解package com.zxp.annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Created by zhangxing on 2017/8/23. */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface SayHi { String say() default ""; }
②在相应的controller进行注解
@SayHi(say = "hello,zhangxing") @GetMapping("/getInfo") public JsonResult getUserInfo(){ List<UserFormMap> userList = userService.getUserInfo(); return new JsonResult(ResultCode.SUCCESS,userList); }
③创建注解拦截器
public class SayInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HandlerMethod handlerMethod = (HandlerMethod) handler; SayHi sayHi =handlerMethod.getMethodAnnotation(SayHi.class); //有sayHi注解 if(sayHi != null){ if(!StringUtils.isEmpty(sayHi.say())){ System.out.println(sayHi.say()); return true; }else{ throw new Exception("跟星哥打招呼了没?"); } }else{ return true; } } }
说明一下,如果访问的controller没有该注解,直接放行;如果有该注解,没有设置内容,则抛出相应的异常;如果有该注解,并且内容也不为空,则执行相应的注解拦截逻辑
④在spring-web中配置注解拦截器
<mvc:annotation-driven/> <!--配置拦截器, 多个拦截器,顺序执行 --> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean class="com.zxp.interceptor.SayInterceptor"></bean> </mvc:interceptor> </mvc:interceptors>
⑤测试
1>有sayHi注解,且内容不为空
2>有sayHi注解,且内容为空
3.测试验证方案
①自定义注解姓名设置注解:
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface NameField { String name() default ""; }
密码设置注解:
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface PwdField { String password() default ""; }
②在相应的属性上进行注解
public class User { private Integer id; @NameField(name = "zhangxing") private String username; @PwdField(password = "zxp52077") private String password; private Date create_time;}
③属性值测试类
public class TestAnnotation { public static void main(String[] args) { getEntityInfo(User.class); } public static void getEntityInfo(Class<?> cls){ Field[] fields = cls.getDeclaredFields(); for (Field field : fields) { //判断是否有name注解 if(field.isAnnotationPresent(NameField.class)){ NameField filedMap = field.getAnnotation(NameField.class); System.out.println("姓名:"+filedMap.name()); } if(field.isAnnotationPresent(PwdField.class)){ PwdField filedMap = field.getAnnotation(PwdField.class); System.out.println("密码:"+filedMap.password()); } } } }
④测试效果
经过一番论证与测试,是不是进一步加深了对注解的认识?它的产生与使用往往跟java 反射机制有着千丝万缕的情愫!
好了,我是张星,欢迎加入博主技术交流群,群号:313145288
相关文章推荐
- javaSE_8系列博客——Java语言的特性(四)--注解--(5)-- 如何使用自定义的注解?
- java注解:如何实现和使用一个自定义注解?
- 如何创建、使用以及解析自定义注解
- SpringAOP使用入门,如何对自定义的注解进行切面编程
- 如何使用java自定义注解?demo
- 如何使用自定义注解。
- 如何添加一个自定义的columnstyles 到设计器中,以便在设计时直接使用他们?
- win2000,xp,2003 如何使用自定义,设置默认纸张
- 如何使用自定义的 number range objects
- 如何使用自定义的 number range objects
- 如何使用自定义的 number range objects
- 如何设计和使用自定义的权限对象(自定义权限检查函数)
- 如何对电子表格组件使用自定义函数
- 关于如何使用自定义的结束消息循环的方式 (转载)
- 如何使用自定义特性和反射?
- 如何自定义类以及如何使用自定义类
- 如何在 Word 中使用自定义样式生成文章目录
- VC中如何使用自定义消息?
- 如何使用Css样式自定义一个DataGrid的风格
- 如何设计和使用自定义的权限对象(自定义权限检查函数)