码迷,mamicode.com
首页 > 编程语言 > 详细

java注解列表

时间:2014-10-29 18:36:48      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   os   ar   java   for   

@Target(targettype)

用于指示那种类型可以应用annotation。

如果一个annotation没有定义target,那么这个annotation可以出现在任何元素上。

target的参数是java.lang.annotation.ElementType.*类型:

  • ANNOTATION_TYPE:annotation
  • CONSTRUCTOR:构造方法
  • FIELD:类field,包括enum
  • LOCAL_VARIABLE:本地变量
  • METHOD:方法
  • PACKAGE:包
  • PARAMETER:参数
  • TYPE:类、接口、annotation、enum

如:

//这个MetaAnnotationType仅能出现在定义annotation时
@Target(ElementType.ANNOTATION_TYPE)
public @interface MetaAnnotationType { ... }

@Retention(tetention-policy)

retention定义annotation失效时机。

其值是java.lang.annotation.RetentionPolicy.*类型,具体取值:

  • source:仅仅在source阶段生效,如@Override、@SuppressWarnings
  • class(默认):在class加载时失效,但在编译成二进制文件时是有效的
  • runtime:不会失效。如:@Deprecated
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

// 此类型annotation不会失效
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
  String stringValue();
  int intValue();
}

public class MainClass {
  // 在运行时,可以获取MyAnnotation的信息
  @MyAnnotation(stringValue = "Annotation Example", intValue = 100)
  public static void myMethod() {
  }
}

 

@Documented

用于指示配置此annotation的java元素,会被javadoc文档化。

@Documented
@Inherited  // for descenders of the annotation to have the @documented feature automatically
@Retention(RetentionPolicy.RUNTIME) // must be there
public @interface InWork {
    ...

/**
 * annotated class 
 */
@InWork(value = "")
public class MainApp {
    ...

如果用javadoc来获取doc文档,会看到MainApp的文档

@Configuration

用于指示class可以被spring ioc容器作为bean的definition

java注解列表

标签:des   style   blog   io   color   os   ar   java   for   

原文地址:http://www.cnblogs.com/ronghantao/p/4059925.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!