1.注意下图,有点特殊的是包里面有个 package-info.java文件,特此说明一下。
此文件的好处是:
1.为标注在包上Annotation提供便利
2.声明友好类和包常量
3.提供包的整体注释说明
该文件内容为:
/**
* 描述:自定义注解包
* 1.为标注在包上Annotation提供便利
* 2.声明友好类和包常量
* 3.提供包的整体注释说明
*
* 注意:这文件里不能声明public的class,即如果 public class Test{} 会报错
*/
package com.xx.xx.web.common.myannotation;
2.自定义注解属性
2.1 @Target 表示该注解目标(作用范围),可能的 ElemenetType 参数包括:
ElemenetType.CONSTRUCTOR 构造器声明
ElemenetType.FIELD 域声明(包括 enum 实例)
ElemenetType.LOCAL_VARIABLE 局部变量声明
ElemenetType.METHOD 方法声明
ElemenetType.PACKAGE 包声明
ElemenetType.PARAMETER 参数声明
ElemenetType.TYPE 类,接口(包括注解类型)或enum声明
2.2 @Retention 表示该注解的生命周期,可选的 RetentionPolicy 参数包括
RetentionPolicy.SOURCE 注解将被编译器丢弃
RetentionPolicy.CLASS 注解在class文件中可用,但会被VM丢弃
RetentionPolicy.RUNTIME VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息
2.3 @Documented 指示将此注解包含在 javadoc 中
2.4 @Inherited 指示允许子类继承父类中的注解
3.下面的自定义注解例子是按照 类、方法、属性注解、使用注解的类、执行类 的顺序依次进行
自定义类注解:
package com.xx.xx.web.common.myannotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 描述:标注在类上面自定义注解
*
* @author
* @create_time
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyClassAnnotation {
// 系统名称
String sysName() default "";
// 唯一标识id
int id();
// 操作描述
String desc() default "";
}
自定义方法注解:
package com.xx.xx.web.common.myannotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 描述:标注在方法上面的自定义注解
*
* @author
* @create_time
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyMethodAnnotation {
String methodDesc() default "该方法没有描述";
}
自定义属性字段注解:
package com.xx.xx.web.common.myannotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 描述:标注在字段、属性上面的自定义注解
*
* @author
* @create_time
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyFieldAnnotation {
String name() default "";
}
使用注解的类:
package com.xx.xx.web.common.myannotation;
/**
* 描述:自定义注解运用类
*
* @author
* @create_time
*/
@MyClassAnnotation(sysName = "电商平台",id = 10,desc = "类注解")
public class TestAnnotation {
@MyFieldAnnotation(name = "字段名称")
private String name;
private int age;//无注解
private String address;//无注解
@MyMethodAnnotation(methodDesc = "getElement方法")
public String getElement() {
return name;
}
// 无方法注解 无实际意义
public int getAgeElement() {
return age;
}
}
测试执行类:
package com.jumore.jbx.web.common.myannotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* 描述:自定义注解执行测试类
*
* @author
* @create_time
*/
public class ExecuteAnnotation {
public static void main(String[] args) {
// 获取类注解
MyClassAnnotation myClassAnnotation = TestAnnotation.class.getAnnotation(MyClassAnnotation.class);
if (myClassAnnotation == null) return;
System.out.println("id = " + myClassAnnotation.id() + "&sysName=" + myClassAnnotation.sysName() + "&desc=" + myClassAnnotation.desc());
System.out.println("**********************类注解end****************************");
System.out.println();
// 获取方法注解
TestAnnotation testAnnotation = new TestAnnotation();
Method[] methods = testAnnotation.getClass().getDeclaredMethods();
for (Method method: methods) {
System.out.println(method.getName());
// 获取该方法的解决
MyMethodAnnotation myMethodAnnotation = method.getAnnotation(MyMethodAnnotation.class);
// 无方法注解的话跳过
if (myMethodAnnotation == null) continue;
System.out.println("methodName = " + method.getName() + "&methodDesc=" + myMethodAnnotation.methodDesc());
}
System.out.println("**********************方法注解end****************************");
System.out.println();
// 获取字段注解
Field[] fields = TestAnnotation.class.getDeclaredFields();
for (Field field: fields) {
System.out.println(field.getName());
// 获取该字段的解决
MyFieldAnnotation myFieldAnnotation = field.getAnnotation(MyFieldAnnotation.class);
if (myFieldAnnotation == null) continue;
System.out.println("fieldName=" + field.getName() + "&name=" + myFieldAnnotation.name());
}
System.out.println("**********************字段注解end****************************");
}
}
执行结果: