JDK 5 Annotation\注解\注释\自定义注解

本文介绍了Java自定义注解的基本概念及其使用方法,包括@Target、@Retention、@Documented和@Inherited等元注解的作用及应用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义注解示例

---------------------------------------------

@Transactional  注解示例

 

Java代码  收藏代码
  1. package  org.springframework.transaction.annotation;  
  2. import  java.lang.annotation.Documented;  
  3. import  java.lang.annotation.ElementType;  
  4. import  java.lang.annotation.Inherited;  
  5. import  java.lang.annotation.Retention;  
  6. import  java.lang.annotation.RetentionPolicy;  
  7. import  java.lang.annotation.Target;  
  8. import  org.springframework.transaction.TransactionDefinition;  
  9.   
  10. @Target ({ElementType.METHOD, ElementType.TYPE}) //可以作用在类上和方法上   
  11. @Retention (RetentionPolicy.RUNTIME) //可以通过反射读取注解   
  12. @Inherited //可以被子类继承   
  13. @Documented //javadoc生成文件档时,包含本注解信息   
  14. public   @interface  Transactional {  
  15.     String value() default   "" ; //使用时没有指定key,值默认赋给value,如:Transactional("abc")   
  16.     Propagation propagation() default  Propagation.REQUIRED;  
  17.     Isolation isolation() default  Isolation.DEFAULT;  
  18.     int  timeout()  default  TransactionDefinition.TIMEOUT_DEFAULT;  
  19.     boolean  readOnly()  default   false ;  
  20.     Class<? extends  Throwable>[] rollbackFor()  default  {};  
  21.     String[] rollbackForClassName() default  {};  
  22.     Class<? extends  Throwable>[] noRollbackFor()  default  {};  
  23.     String[] noRollbackForClassName() default  {};  
  24. }  
 

 

 

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

---------------------------------------------

它们是Target、Retention、Documented和Inherited。

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

 

@Inherited

---------------------------------------------

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

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

 

Java代码  收藏代码
  1. 自大定义一个MyAnnotation注解  
  2. @Inherited   
  3. @interface  MyAnnotation { }  
  4.   
  5. 使用MyAnnotation注解  
  6. @MyAnnotation   
  7. public   class  ParentClass {}  
  8.   
  9. 子类继承父类  
  10. public   class  ChildClass  extends  ParentClass { }  

 

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

 

@Retention 

---------------------------------------------

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

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

 

Java代码  收藏代码
  1. @Retention (RetentionPolicy.SOURCE)  
  2. @interface  MyAnnotation1 { }  
  3. //作用是不将注解保存在class文件中,也就是说象“//”一样在编译时被过滤掉了。   
  4.   
  5. @Retention (RetentionPolicy.CLASS)  
  6. @interface  MyAnnotation2 {}  
  7. //作用是只将注解保存在class文件中,而使用反射读取注解时忽略这些注解。   
  8.   
  9. @Retention (RetentionPolicy.RUNTIME)  
  10. @interface  MyAnnotation3 {}  
  11. //作用是即将注解保存在class文件中,也可以通过反射读取注解。这也是最常用的值    
 

 

 

@Target

---------------------------------------------

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

 

@Documented  

---------------------------------------------

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

 

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

---------------------------------------------

 

Java代码  收藏代码
  1. //取得类上的指定的注解   
  2. Annotation annotation = 类.class .getAnnotation(MyAnnotation. class );  
  3.   
  4. //取得类上的所有注解,包括继承的注解。   
  5. Annotation[] annotations = 类.class .getAnnotations();  
  6.   
  7. //取当前类上的所有的注解,不包括继承的   
  8. Annotation[] annotations = 类.class .getDeclaredAnnotations();  
 

 

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

---------------------------------------------

 

Java代码  收藏代码
  1. //取得方法上的指定的注解   
  2. Method m=?  
  3. Annotation annotation = m.getAnnotation(MyAnnotation.class );  
  4.   
  5. //取得方法上的所有注解,包括继承的注解。   
  6. Annotation[] annotations = m.getAnnotations();  
  7.   
  8. //取当前方法上的所有的注解,不包括继承的   
  9. Annotation[] annotations = m.getDeclaredAnnotations();  
  

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值