您的位置:首页 > 编程语言 > Java开发

Java自定义注解

2015-01-23 18:37 411 查看
java注解是附加在代码中的一些元信息,用于一些工具在编译、运行时进行解析和使用,起到说明、配置的功能。
注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用。包含在 java.lang.annotation 包中。1、元注解元注解是指注解的注解。包括 @Retention @Target @Document @Inherited四种。
1.1、@Retention: 定义注解的保留策略@Retention(RetentionPolicy.SOURCE) //注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS) // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得,
@Retention(RetentionPolicy.RUNTIME) // 注解会在class字节码文件中存在,在运行时可以通过反射获取到

1.2、@Target:定义注解的作用目标
其定义的源码为:
[java] view
plaincopy

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.ANNOTATION_TYPE)

public @interface Target {

ElementType[] value();

}

@Target(ElementType.TYPE) //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
由以上的源码可以知道,他的elementType 可以有多个,一个注解可以为类的,方法的,字段的等等

1.3、@Document:说明该注解将被包含在javadoc中

1.4、@Inherited:说明子类可以继承父类中的该注解

2、java 注解的自定义
下面是自定义注解的一个例子
[java] view
plaincopy

@Documented

@Target({ElementType.TYPE,ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

public @interface Yts {

public enum YtsType{util,entity,service,model}

public YtsType classType() default YtsType.util;

}

[java] view
plaincopy

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

@Inherited

public @interface HelloWorld {

public String name()default "";

}

@Retention(RetentionPolicy.RUNTIME)定义的这个注解是注解会在class字节码文件中存在,在运行时可以通过反射获取到。@Target({ElementType.TYPE,ElementType.METHOD})因此这个注解可以是类注解,也可以是方法的注解这样一个注解就自定义好了,当然注解里面的成员可以为基本的数据类型,也可以为数据,Object等等 3 注解是定义好了,那么怎么来得到,解析注解呢?java的反射机制可以帮助,得到注解,代码如下:[java] view
plaincopy

public class ParseAnnotation {

public void parseMethod(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException{

Object obj = clazz.getConstructor(new Class[]{}).newInstance(new Object[]{});

for(Method method : clazz.getDeclaredMethods()){

HelloWorld say = method.getAnnotation(HelloWorld.class);

String name = "";

if(say != null){

name = say.name();

method.invoke(obj, name);

}

Yts yts = (Yts)method.getAnnotation(Yts.class);

if(yts != null){

if(YtsType.util.equals(yts.classType())){

System.out.println("this is a util method");

}else{

System.out.println("this is a other method");

}

}

}

}

@SuppressWarnings("unchecked")

public void parseType(Class clazz) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{

Yts yts = (Yts) clazz.getAnnotation(Yts.class);

if(yts != null){

if(YtsType.util.equals(yts.classType())){

System.out.println("this is a util class");

}else{

System.out.println("this is a other class");

}

}

}

}

前一个方法是解析得到方法注解的,后一个方法是得到类注解的以下是测试方法类
[java] view
plaincopy

@Yts(classType =YtsType.util)

public class SayHell {

@HelloWorld(name = " 小明 ")

@Yts

public void sayHello(String name){

if(name == null || name.equals("")){

System.out.println("hello world!");

}else{

System.out.println(name + "say hello world!");

}

}

}

[html] view
plaincopy

public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException, InstantiationException {

ParseAnnotation parse = new ParseAnnotation();

parse.parseMethod(SayHell.class);

parse.parseType(SayHell.class);

}

转载地址:http://blog.csdn.net/yixiaogang109/article/details/7328466

使用注解

在一般的Java开发中,最常接触到的可能就是@Override@SupressWarnings这两个注解了。使用@Override的时候只需要一个简单的声明即可。这种称为标记注解(marker
annotation ),它的出现就代表了某种配置语义。而其它的注解是可以有自己的配置参数的。配置参数以名值对的方式出现。使用 @SupressWarnings的时候需要类似@SupressWarnings({"uncheck", "unused"})这样的语法。在括号里面的是该注解可供配置的值。由于这个注解只有一个配置参数,该参数的名称默认为value,并且可以省略。而花括号则表示是数组类型。在JPA中的@Table注解使用类似@Table(name
= "Customer", schema = "APP")这样的语法。从这里可以看到名值对的用法。在使用注解时候的配置参数的值必须是编译时刻的常量。从某种角度来说,可以把注解看成是一个XML元素,该元素可以有不同的预定义的属性。而属性的值是可以在声明该元素的时候自行指定的。在代码中使用注解,就相当于把一部分元数据从XML文件移到了代码本身之中,在一个地方管理和维护。

开发注解

在一般的开发中,只需要通过阅读相关的API文档来了解每个注解的配置参数的含义,并在代码中正确使用即可。在有些情况下,可能会需要开发自己的注解。这在库的开发中比较常见。注解的定义有点类似接口。下面的代码给出了一个简单的描述代码分工安排的注解。通过该注解可以在源代码中记录每个类或接口的分工和进度情况。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Assignment {
String assignee();
int effort();
double finished() default 0;
}
@interface用来声明一个注解,其中的每一个方法实际上是声明了一个配置参数。方法的名称就是参数的名称,返回值类型就是参数的类型。可以通过default来声明参数的默认值。在这里可以看到@Retention@Target这样的元注解,用来声明注解本身的行为。@Retention用来声明注解的保留策略,有CLASSRUNTIMESOURCE这三种,分别表示注解保存在类文件、JVM运行时刻和源代码中。只有当声明为RUNTIME的时候,才能够在运行时刻通过反射API来获取到注解的信息。@Target用来声明注解可以被添加在哪些类型的元素上,如类型、方法和域等。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java自定义注解