博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java中的注解(二)
阅读量:7180 次
发布时间:2019-06-29

本文共 3913 字,大约阅读时间需要 13 分钟。

    今天我继续来介绍java中的注解。注解与接口和类不同的是注解是不允许继承的,但是注解中有一个和继承有关的元注解:@Inherited。如果我们在定义注解时候加上这个元注解那么我们就可以在子类中监测到该注解的存在。

@Target(ElementType.TYPE)@Inherited@Retention(RetentionPolicy.RUNTIME)public @interface Action {    boolean isSingle() default true;    String value() default "";    String url() default "";}@Actionclass BaseController{    }public class UserController extends BaseController{        public static void main(String[] args) {        System.out.println(UserController.class.isAnnotationPresent(Action.class)); //true        System.out.println(BaseController.class.isAnnotationPresent(Action.class)); //true    } }

如上代码所示:我在BaseController上加了Action的注解但是没有在UserController上加但是我却可以在UserController类上检查到Action注解的存在就是因为我们在定义注解时加了@Inherited。

    我们现在已经可以自己创建注解,接下来我来给大家介绍如何获取注解的信息。在java中是通过反射来获取注解的信息的,在Class,Method,Field,Constructor中都有如下的这几个方法:getAnnotations()获取所有的注解,getDeclaredAnnotations获取所有声明的注解,忽略inherited来的注解,getAnnotations(Class<A> annotationclass)获取指定类型的注解,isAnnotationPresent(Class<A> annotationclass)判断是否有指定类型的注解。下面我来通过代码来获取类上的注解:

@Target(ElementType.TYPE)@Inherited@Retention(RetentionPolicy.RUNTIME)@interface Action {    boolean isSingle() default true;    String value() default "";    String url() default "";}@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@interface Service {    String value() default "";    String url() default "";}@Action(value="action",url="actionurl")@Service(value="service",url="serviceurl")class BaseController{    }public class UserController extends BaseController{        public static void main(String[] args) throws ClassNotFoundException {        Class
cls = BaseController.class; Annotation[] annotations = cls.getAnnotations(); for (Annotation annotation : annotations) { if(annotation instanceof Action){ Action action = (Action)annotation; System.out.println(action.url()); //actionurl System.out.println(action.value()); //action } if(annotation instanceof Service){ Service service = (Service)annotation; System.out.println(service.url()); //serviceurl System.out.println(service.value()); //service } } } }

在java中Method和Contructor都有方法参数,而参数也是可以有注解的,所以有一个方法返回参数的注解:getParameterAnnotations()返回值是一个二维数组。我们来看一个例子:

public class TestMethodAnnotations {    @Target(ElementType.PARAMETER)    @Retention(RetentionPolicy.RUNTIME)    static @interface requestParam {    String value();    }    @Target(ElementType.PARAMETER)    @Retention(RetentionPolicy.RUNTIME)    static @interface DefaultValue {    String value() default "";    }        public void test(@requestParam("useranme") String username,            @requestParam("password") @DefaultValue("1234") String password){            }        public static void main(String[] args) throws NoSuchMethodException, SecurityException {        Class
cls = TestMethodAnnotations.class; Method method = cls.getMethod("test",new Class[]{String.class,String.class}); Annotation[][] anns = method.getParameterAnnotations(); for (Annotation[] annotations : anns) { for (Annotation annotation : annotations) { if(annotation instanceof requestParam){ requestParam request = (requestParam)annotation; System.out.println(request.value()); } if(annotation instanceof DefaultValue){ DefaultValue defaultValue = (DefaultValue)annotation; System.out.println(defaultValue.value()); } } } } }

代码还是比较的简单的,在这个例子中我们使用@requestParam和@DefaultValue两个注解来修饰方法的参数然后使用getParameterAnnotations这个方法来获取方法参数上的注解。

    通过注解我们可以实现很多的功能,比如依赖注入,绑定参数等等,掌握注解的使用有利于我们日常的开发并且为日后学习框架打下基础。

转载于:https://www.cnblogs.com/suyang-java/p/10886092.html

你可能感兴趣的文章
elk安装(这个是初级的可以把这个套件安上)
查看>>
thinkphp验证码(总结之后,效率非常好)
查看>>
网络流量分析——NPMD关注IT运维、识别宕机和运行不佳进行性能优化。智能化分析是关键-主动发现业务运行异常。科来做APT相关的安全分析...
查看>>
.NET接入UnionPay银联支付(一)手机wap支付
查看>>
Java多线程-工具篇-BlockingQueue
查看>>
js中动态创建json,动态为json添加属性、属性值的实例
查看>>
使用Qemu运行Ubuntu文件系统(1)
查看>>
红黑树
查看>>
2018年10月小结(流水账) -- 1024程序员节快乐
查看>>
SpringBoot(八)配置logback日志
查看>>
单点登录 之 OAuth
查看>>
『流畅的Python』第15章:上下文管理器和else块
查看>>
windows环境下面批量新建文件夹
查看>>
MS CRM 2011 如何获得当前用户使用的界面语言
查看>>
敏捷个人俱乐部(北京)线下活动 开始报名了!
查看>>
IPMSG飞鸽传书——编译源代码的方法
查看>>
80后的我们
查看>>
C语言文件操作解析(四)
查看>>
Windows Azure Cloud Service (25) Windows Azure 新的增强功能
查看>>
MVC开发人员必须拥有的五大工具
查看>>