Spring自定义注解的使用
参考文献:自定义注释@interface的用法理解
参考文献:java元注解 @Documented注解使用
参考文献:@Retention注解作用
参考文献:@Target:注解的作用目标
一、@Target:注解的作用目标
使用方法 | 作用目标 |
---|---|
@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) | 包 |
二、@Retention:注解的保留位置
使用方法 | 说明 |
---|---|
RetentionPolicy.SOURCE | 这种类型的Annotations只在源代码级别保留,编译时就会被忽略,在class字节码文件中不包含。 |
RetentionPolicy.CLASS | 这种类型的Annotations编译时被保留,默认的保留策略,在class文件中存在,但JVM将会忽略,运行时无法获得。 |
RetentionPolicy.RUNTIME | 这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用。 |
三、@Document:说明该注解将被包含在javadoc中
1.参考转载:java元注解 @Documented注解使用
2.注解表明这个注解应该被 javadoc工具记录. 默认情况下,javadoc是不包括注解的. 但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的工具处理, 所以注解类型信息也会被包括在生成的文档中,是一个标记注解,没有成员。
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Column {
public String name() default "fieldName";
public String setFuncName() default "setField";
public String getFuncName() default "getField";
public boolean defaultDBValue() default false;
}
四、@interface:自定义注释的用法理解
参考转载:自定义注释@interface的用法理解
场景:为了理解@interface使用
1.@interface自定义注解
- 1.1 @interface自定义注解自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节。
- 1.2 在定义注解时,不能继承其他的注解或接口。
- 1.3 使用@interface来声明一个注解,
- 1.3.1 每一个方法实际上是声明了一个配置参数,
- 1.3.2 方法的名称就是参数的名称,
- 1.3.3 返回值类型就是参数的类型,(返回值类型只能是基本类型、Class、String、enum)
- 1.3.4 可以通过default来声明参数的默认值
2.举例说明,分别作用在类,方法,属性上的注解
- 2.1 作用在属性上注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FiledAnnotation {
String value() default "GetFiledAnnotation";
}
- 2.2 作用在方法上注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodAnnotation {
String name() default "MethodAnnotation";
String url() default "https://www.cnblogs.com";
}
- 2.3 作用在类上注解
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TypeAnnotation {
String value() default "Is-TypeAnnotation";
}
- 2.4 使用自定义注解
@TypeAnnotation(value = "doWork")
public class Worker {
@FiledAnnotation(value = "CSDN博客")
private String myfield = "";
@MethodAnnotation()
public String getDefaultInfo() {
return "do the getDefaultInfo method";
}
@MethodAnnotation(name = "百度", url = "www.baidu.com")
public String getDefineInfo() {
return "do the getDefineInfo method";
}
}
- 2.5 测试自定义注解
public class TestMain {
public static void main(String[] args) throws Exception {
Class cls = Class.forName("com.zbz.annotation.pattern3.Worker");
Method[] method = cls.getMethods();
/**判断Worker类上是否有TypeAnnotation注解*/
boolean flag = cls.isAnnotationPresent(TypeAnnotation.class);
/**获取Worker类上是TypeAnnotation注解值*/
if (flag) {
TypeAnnotation typeAnno = (TypeAnnotation) cls.getAnnotation(TypeAnnotation.class);
System.out.println("@TypeAnnotation值:" + typeAnno.value());
}
/**方法上注解*/
List<Method> list = new ArrayList<Method>();
for (int i = 0; i < method.length; i++) {
list.add(method[i]);
}
for (Method m : list) {
MethodAnnotation methodAnno = m.getAnnotation(MethodAnnotation.class);
if (methodAnno == null)
continue;
System.out.println( "方法名称:" + m.getName());
System.out.println("方法上注解name = " + methodAnno.name());
System.out.println("方法上注解url = " + methodAnno.url());
}
/**属性上注解*/
List<Field> fieldList = new ArrayList<Field>();
for (Field f : cls.getDeclaredFields()) {// 访问所有字段
FiledAnnotation filedAno = f.getAnnotation(FiledAnnotation.class);
System.out.println( "属性名称:" + f.getName());
System.out.println("属性注解值FiledAnnotation = " + filedAno.value());
}
}
}