前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >秒懂,Java 注解 (Annotation)你可以这样用

秒懂,Java 注解 (Annotation)你可以这样用

作者头像
lyb-geek
发布2018-07-26 10:18:35
8610
发布2018-07-26 10:18:35
举报
文章被收录于专栏:Linyb极客之路

自定义注解示例

@Transactional 注解示例

代码语言:javascript
复制
package org.springframework.transaction.annotation;  
import java.lang.annotation.Documented;  
import java.lang.annotation.ElementType;  
import java.lang.annotation.Inherited;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  
import org.springframework.transaction.TransactionDefinition;  

@Target({ElementType.METHOD, ElementType.TYPE})//可以作用在类上和方法上  
@Retention(RetentionPolicy.RUNTIME)//可以通过反射读取注解  
@Inherited//可以被子类继承  
@Documented//javadoc生成文件档时,包含本注解信息  
public @interface Transactional {  
    String value() default "";//使用时没有指定key,值默认赋给value,如:Transactional("abc")  
    Propagation propagation() default Propagation.REQUIRED;  
    Isolation isolation() default Isolation.DEFAULT;  
    int timeout() default TransactionDefinition.TIMEOUT_DEFAULT;  
    boolean readOnly() default false;  
    Class<? extends Throwable>[] rollbackFor() default {};  
    String[] rollbackForClassName() default {};  
    Class<? extends Throwable>[] noRollbackFor() default {};  
    String[] noRollbackForClassName() default {};  
}

J2SE5.0为注解单独提供了4种注解

它们是Target、Retention、Documented和Inherited。

可以在自定义注解时使用这4个注解。

@Inherited

继承是java主要的特性之一。在类中的protected和public成员都将会被子类继承,但是父类的注解会不会被子类继承呢?

很遗憾的告诉大家,在默认的情况下,父类的注解并不会被子类继承。如果要让这个注解可以被继承,就必须在定义注解时在源码上加上@Inherited注解。

代码语言:javascript
复制
自大定义一个MyAnnotation注解  
@Inherited  
@interface MyAnnotation { }  

使用MyAnnotation注解  
@MyAnnotation  
public class ParentClass {}  

子类继承父类  
public class ChildClass extends ParentClass { }

在以上代码中ChildClass和ParentClass一样都已被MyAnnotation注解了。

@Retention

成功通过反射读取类上或方法上的注解,是有前提的--要使用@Retention,就是设置注解是否保存在class文件中。

下面的代码是Retention的详细用法。

代码语言:javascript
复制
@Retention(RetentionPolicy.SOURCE)  
@interface MyAnnotation1 { }  
//作用是不将注解保存在class文件中,也就是说象“//”一样在编译时被过滤掉了。  

@Retention(RetentionPolicy.CLASS)  
@interface MyAnnotation2 {}  
//作用是只将注解保存在class文件中,而使用反射读取注解时忽略这些注解。  

@Retention(RetentionPolicy.RUNTIME)  
@interface MyAnnotation3 {}  
//作用是即将注解保存在class文件中,也可以通过反射读取注解。这也是最常用的值

@Target

标识自定义注解 可以作用于 类上、方法上、成员变量上、构造方法、其它...

@Documented

在默认的情况下在使用javadoc自动生成文档时,注解将被忽略掉。如果想在文档中也包含注解,必须使用Documented为文档注解。

如何读取注解--类上的注解

代码语言:javascript
复制
//取得类上的指定的注解  
Annotation annotation = 类.class.getAnnotation(MyAnnotation.class);  

//取得类上的所有注解,包括继承的注解。  
Annotation[] annotations = 类.class.getAnnotations();  

//取当前类上的所有的注解,不包括继承的  
Annotation[] annotations = 类.class.getDeclaredAnnotations();

如何读取注解--方法上的注解

代码语言:javascript
复制
//取得方法上的指定的注解  
Method m=?  
Annotation annotation = m.getAnnotation(MyAnnotation.class);  

//取得方法上的所有注解,包括继承的注解。  
Annotation[] annotations = m.getAnnotations();  

//取当前方法上的所有的注解,不包括继承的  
Annotation[] annotations = m.getDeclaredAnnotations();

注:要想使用反射得到注解信息,这个注解在定义时源码中必须使用

@Retention(RetentionPolicy.RUNTIME)进行注解。

注解基础知识点汇总

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-06-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Linyb极客之路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 自定义注解示例
  • J2SE5.0为注解单独提供了4种注解
    • @Inherited
      • @Retention
        • @Target
          • @Documented
          • 如何读取注解--类上的注解
          • 如何读取注解--方法上的注解
          • 注解基础知识点汇总
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档