Custom annotation classes and usage instance parsing in Java

This article mainly introduces the user-defined annotation classes in Java and the use of process parsing. It is introduced in great detail through the example code, which has a certain reference value for everyone's study or work. Friends in need can refer to it

In the Java framework, annotations are often used, and you can save a lot of things to understand the custom annotations.

Annotations are metadata that can be added to Java code. Classes, methods, variables, parameters and packages can be modified with annotations. Annotations have no direct impact on the code they decorate

First write your own annotation class

@Documented //会被javadoc命令识别
@Retention(RetentionPolicy.RUNTIME) //相当于作用时期,比如:运行期、编译期
@Target({ElementType.METHOD}) //相当于作用域,比如方法、类
public @interface MyValue {

  String value();
  //也可以这么写,就是说,它的默认值是hello
  //String value() default "hello";

}

Then analyze the two classes used above:

public enum RetentionPolicy {
  SOURCE,CLASS,RUNTIME
}
public enum ElementType {
  /** Class,interface (including annotation type),or enum declaration */
  TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE,ANNOTATION_TYPE,PACKAGE
}

You can see that there are two enumeration classes, that is, our custom annotations have a certain time and space scope. Well, our user-defined annotation has been completed (yes, the user-defined annotation is the code above). Let's see how it can be useful. It's easy. Just like other annotations, it's just written where we need to use it. (yes, I'm sure I'm not kidding)

public class Person {

  @MyValue(value="张三")
  private String name;

  /*
  为什么要写setter和getter,很快你就会知道
   */
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

Then we'll get it somewhere else

Person person = new Person();
System.out.println(person.getName());//null
//这就很难受,按道理来说,不是应该是张三吗?

As we all know, frameworks like spring are implemented through reflection. We simulate a "compiled class". We use annotations on properties, so we use reflection to get all properties of the class first

public static void main(String[] args) throws NoSuchFieldException {
    Person person = new Person();

    //按理来说,我们是拿到这个Person.class的所有的属性,然后遍历,来挨个注入,但是这里我们明明确我们的属性名,所以就简单化了
    Field  field = Person.class.getDeclaredField("name");

    MyValue annotation = field.getAnnotation(MyValue.class);//拿到注解类

    String name = annotation.value();//这个value()就是我们在MyValue类中的的属性

    //然后我们就注入到这个类中,这时就用到了setter方法
    person.setName(name);

    System.out.println("通过自定义注解后的person的name是:" + person.getName());
  }

Yes, in this way, we injected a name attribute into person through custom annotation, but it can't be so complex in practical application. This is just an introduction. We can assemble this "simulated compilation class" into a tool class for our practical application.

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>