• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

深入理解Java注解的使用方法

java 搞代码 4年前 (2022-01-05) 38次浏览 已收录 0个评论

这篇文章主要为大家详细介绍了Java注解的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

注解是jdk1.5新增的特性.大家都知道,jdk1.5在java的发展史上有着划时代的意义.而注解的出现,在某种程度上颠覆了框架的设计.比如,spring在注解出现后,改善了原先五大组件的模式,增加了基于注解的实现方式.现在重点讲讲注解的使用.

元注解:

      jdk1.5定义了4个元注解,元注解的作用是注解其他的注解.

       1.@Retention

       2.@Target

       3.@Documented

       4.@Inherited

       @Retention用于指明该注解存在的时机.参数有三个值可选:RetentionPolicy.SOURCE,RetentionPolicy.CLASS,RetentionPolicy.RUNTIME可供选择.分别表示:源码中保留注解,字节码文件中保留注解,运行时保留注解.

       @Target用于指明注解能作用的范围.比如参数中设置为ElementType.TYPE,表示作用于类和接口.如果你用来注解方法,则会发生编译错误.由此可见它的功能是通过编译器实现的.

       @Documented表明该注解在使用javadoc工具生成开发文档时,也会被纳入进去.

   来源gaodaimacom搞#代%码网    @Inherited表明,某个位置使用该注解,那么在存在Java继承关系的地方,该注解也能被继承过来.这个可能不好理解.下面的代码加以说明.     

 @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE,ElementType.METHOD}) @Inherited public @interface AnnoInherited { } 

测试代码:

 public class TestAnnoInherated { public static void main(String[] args) { Annotation[] annos=new Goo().getClass().getAnnotations(); for(Annotation a:annos){ System.out.println(a); } } } @AnnoInherited class Foo{ } class Goo extends Foo{ } 

控制台输出:

@test.annotation.AnnoInherited()

上例中Goo前面并没有加注解@AnnoInherited,但是父类Foo前面有,而@AnnoInherited加了元注解@Inherited,所以Foo能继承过来.

自定义注解:

自定义注解的实例如下.

 package test.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; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface AnimalInfo { String shout() default ""; //能不能看门 boolean isGuard() default true; } 

测试代码:

 public class TestAnimalInfo { public static void main(String[] args) { Animal animal=new Animal(); AnimalInfo info=animal.getClass().getAnnotation(AnimalInfo.class); if(info!=null){ Annotation anno=info;//此处并没有报错.Annotation是一个接口.info是一个注解.这是因为编译器会将注解编译成接口,并且继承了Annotation System.out.println("Annotation类信息:"+Annotation.class); System.out.println("AnimalInfo类信息:"+AnimalInfo.class); Class[] cs=AnimalInfo.class.getInterfaces(); for(Class c:cs){ System.out.println(c); //AnimalInfo编译后就是一个接口,并且继承了Annotation,这里得到了证实. } System.out.println("info对象的类信息:"+info.getClass()); if("wangwang".equals(info.shout())&&info.isGuard()){ System.out.println("the animal is a dog"); }else if("miaomiao".equals(info.shout())&&!info.isGuard()){ System.out.println("the animal is a cat"); }else{ System.out.println("the animal is not a dog or cat"); } }else{ System.out.println("it's not a animal"); } } } @AnimalInfo(shout="wangwang",isGuard=true) class Animal{ } 

控制台输出:

 Annotation类信息:interface java.lang.annotation.Annotation AnimalInfo类信息:interface test.annotation.AnimalInfo interface java.lang.annotation.Annotation info对象的类信息:class com.sun.proxy.$Proxy1 the animal is a dog

代码分析:从控制台可以看到.@AnimalInfo注解其实编译后就是接口,并且它继承了Annnotation.而通过反射获得的注解实例,名字为$Proxy1,是一个类的对象.可见,该注解实例是JVM通过动态代理技术生成的.这也揭示了注解特性的底层实现原理.关于注解的具体底层技术原理,这里不再详谈.

以上就是深入理解Java注解的使用方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:深入理解Java注解的使用方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址