自定义注解

需求是将增删改信息记录在日志里面这里涉及到的有Spring Aop及反射的引用,考虑到项目仅是记录操作日志,切面通过事务处理的方式的定义。

具体的反射、aop切面定义后面再做总结,这里仅记录自定义注解。

java.lang.annotation提供了四种元注解,专门注解其他的注解(在自定义注解的时候,需要使用到元注解):
   @Documented –注解是否将包含在JavaDoc中
   @Retention –什么时候使用该注解
   @Target –注解用于什么地方
   @Inherited – 是否允许子类继承该注解

  1.)@Retention– 定义该注解的生命周期
  ●   RetentionPolicy.SOURCE : 在编译阶段丢弃。这些注解在编译结束之后就不再有任何意义,所以它们不会写入字节码。@Override, @SuppressWarnings都属于这类注解。
  ●   RetentionPolicy.CLASS : 在类加载的时候丢弃。在字节码文件的处理中有用。注解默认使用这种方式
  ●   RetentionPolicy.RUNTIME : 始终不会丢弃,运行期也保留该注解,因此可以使用反射机制读取该注解的信息。我们自定义的注解通常使用这种方式。

  2.)Target – 表示该注解用于什么地方。默认值为任何元素,表示该注解用于什么地方。可用的ElementType参数包括
  ● ElementType.CONSTRUCTOR:用于描述构造器
  ● ElementType.FIELD:成员变量、对象、属性(包括enum实例)
  ● ElementType.LOCAL_VARIABLE:用于描述局部变量
  ● ElementType.METHOD:用于描述方法
  ● ElementType.PACKAGE:用于描述包
  ● ElementType.PARAMETER:用于描述参数
  ● ElementType.TYPE:用于描述类、接口(包括注解类型) 或enum声明

 3.)@Documented–一个简单的Annotations标记注解,表示是否将注解信息添加在java文档中。

 4.)@Inherited – 定义该注释和子类的关系
     @Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。

 属性的自定义注解编写

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface FieldRemark {
    public String value() default "";
    public String dictType() default "";
    public String relationAttribute() default "";
}

类的自定义注解编写

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface PojoRemark {
    public String value() default "";
}

自定义注解类编写的一些规则:
  1. Annotation型定义为@interface, 所有的Annotation会自动继承java.lang.Annotation这一接口,并且不能再去继承别的类或是接口.
  2. 参数成员只能用public或默认(default)这两个访问权修饰
  3. 参数成员只能用基本类型byte,short,char,int,long,float,double,boolean八种基本数据类型和String、Enum、Class、annotations等数据类型,以及这一些类型的数组.
  4. 要获取类方法和字段的注解信息,必须通过Java的反射技术来获取 Annotation对象,因为你除此之外没有别的获取注解对象的方法
  5. 注解也可以没有定义成员, 不过这样注解就没啥用了
PS:自定义注解需要使用到元注解

package testAnnotation;

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

@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface Person{
    String name();
    int age();
}

一般来说,注解都是搭配反射的解析器共同工作的。然后利用反射机制查看类的注解内容

package testAnnotation;

@Person(name="xingoo",age=25)
public class test3 {
    public static void print(Class c){
        System.out.println(c.getName());
        
        //java.lang.Class的getAnnotation方法,如果有注解,则返回注解。否则返回null
        Person person = (Person)c.getAnnotation(Person.class);
        
        if(person != null){
            System.out.println("name:"+person.name()+" age:"+person.age());
        }else{
            System.out.println("person unknown!");
        }
    }
    public static void main(String[] args){
        test3.print(test3.class);
    }
}

运行结果,读取到了注解的内容

testAnnotation.test3
name:xingoo age:25

 

原文地址:https://www.cnblogs.com/person008/p/9430123.html