java自定义注解简单使用

时间:2022-05-21 12:00:49

  元注解的作用就是负责注解其他注解。Java5.0定义了4个标准的meta-annotation类型,它们被用来提供对其它 annotation类型作说明。Java5.0定义的元注解:
  
  1.@Target  
    表示该注解用于那个地方(如果不写包括下面所有的类型,注解可以用于任意类型上面)
  2.@Retention
    表示该注解被保留的时间长短
  3.@Documented
    表示生成javaDoc的时候会将方法的详细信息也生成
  4.@Inherited
    表示被他修饰的注解具有继承性,子类会继承父类的注解(默认没有继承性)
    
下面用一个实例demo来说说明(代码中有详细说明 以及测试):

结构:

java自定义注解简单使用

Annotation01:

package cn.lijie.an01;

import java.lang.annotation.*;

/**
* JDK4种元注解
* ----------------------------------------------
*
* 1.Retention 表示该注解被保留的时间长短
*编译器将注解记录在class文件中, 运行java程序时, jvm不会保留(默认)
* @Retention(RetentionPolicy.CLASS)
* 编译器将注解记录在class文件中, 运行java程序时, jvm会保留, 可以通过反射获取注解
* @Retention(RetentionPolicy.RUNTIME)
* 编译器直接丢弃策略注解
* @Retention(RetentionPolicy.SOURCE)
* ----------------------------------------------
*
* 2.Target 表示该注解用于那个地方(如果不写包括下面所有的类型,注解可以用于任意类型上面)
* 类,接口,enum声明
* @Target(ElementType.TYPE)
* 方法声明
* @Target(ElementType.METHOD)
* 注解声明
* @Target(ElementType.ANNOTATION_TYPE)
* 构造函数声明
* @Target(ElementType.CONSTRUCTOR)
* 域声明
* @Target(ElementType.FIELD)
* 局部变量声明
* @Target(ElementType.LOCAL_VARIABLE)
* 包声明
* @Target(ElementType.PACKAGE)
* 参数声明
* @Target(ElementType.PARAMETER)
* ----------------------------------------------
*
* 3.Documented 表示生成javaDoc的时候会将方法的详细信息也生成
* @Documented
* ----------------------------------------------
*
* 4.Inherited 表示被他修饰的注解具有继承性,子类会继承父类的注解(默认没有继承性)
* @Inherited
* ----------------------------------------------
*
*/

@Inherited
@Documented
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation01 {
String name();
}

Annotation02:

package cn.lijie.an01;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation02 {
public long id();
}

MyTest:

package cn.lijie.an01;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
* Created by jie on 2017/8/16.
*/

@Annotation02(id = 10086L)
@Annotation01(name = "MyTest")
public class MyTest {

@Annotation01(name = "mytest01")
public void myTest01() {

}

@Annotation01(name = "myTest02")
public void myTest02() {

}

public static void main(String[] args) {
classAnnotation();
methodAnnotation();
}

/**
* 类注解测试
*/

private static void classAnnotation() {
//是否存在注解
boolean b1 = MyTest.class.isAnnotationPresent(Annotation01.class);
//获取注解上面的值 ,当获取不到会返回null
Annotation01 a1 = MyTest.class.getAnnotation(Annotation01.class);
//获取全部注解
final Annotation[] annotations = MyTest.class.getAnnotations();
if (b1) {
System.out.println(a1.name());
}
for (Annotation an : annotations) {
System.out.println(an.annotationType());
}
}

/**
* 方法注解测试
*/

private static void methodAnnotation() {
Method[] methods = MyTest.class.getMethods();
for (Method method : methods) {
boolean m1 = method.isAnnotationPresent(Annotation01.class);
//当获取不到会返回null
Annotation01 m2 = method.getAnnotation(Annotation01.class);
if (m1) {
System.out.println(m2.name());
}
}
}
}

MyTestSon:

package cn.lijie.an01;

/**
* Created by jie on 2017/8/16.
*/

public class MyTestSon extends MyTest {
public static void main(String[] args) {

//当父类MyTest所用注解使用@Inherited的时候就能获取到父类的注解
boolean a1 = MyTestSon.class.isAnnotationPresent(Annotation01.class);
if(a1){
//当获取不到会返回null
Annotation01 annotation = MyTestSon.class.getAnnotation(Annotation01.class);
System.out.println(annotation.name());
}
}
}

执行MyTest:

java自定义注解简单使用

执行MyTestSon:

java自定义注解简单使用