Skip to content
On this page

Use annotations

What is annotation? Annotations are special "comments" placed before classes, methods, fields, and parameters in Java source code:

java
// this is a component:
@Resource("hello")
public class Hello {
  @Inject
  int n;

  @PostConstruct
  public void hello(@Param String name) {
    System.out.println(name);
  }

  @Override
  public String toString() {
    return "Hello";
  }
}

Annotations will be directly ignored by the compiler, and annotations can be packaged into class files by the compiler. Therefore, annotations are a kind of "metadata" used as annotations.

The role of annotations

From the JVM's perspective, annotations themselves have no impact on code logic. How to use annotations is entirely determined by the tool.

Java annotations can be divided into three categories:

The first category is annotations used by the compiler, for example:

  • @Override : Let the compiler check whether the method implements override correctly;
  • @SuppressWarnings : Tells the compiler to ignore warnings generated by the code here.

Such annotations will not be compiled into the .class file, they will be thrown away by the compiler after compilation.

The second type is annotations used by tools to process .class files. For example, some tools will dynamically modify the class when loading the class to implement some special functions. Such annotations will be compiled into the .class file, but will not exist in memory after loading. This type of annotation is only used by some underlying libraries, and generally we don't have to deal with it ourselves.

The third category is annotations that can be read during program running. They always exist in the JVM after loading. This is also the most commonly used annotation. For example, a method configured with @PostConstruct will be automatically called after calling the constructor (this is a function implemented by the Java code to read the annotation, and the JVM will not recognize the annotation).

When defining an annotation, you can also define configuration parameters. Configuration parameters can include:

  • All basic types;
  • String;
  • enumeration type;
  • Arrays of basic types, String, Class, and enumerations.

Because configuration parameters must be constants, the above restrictions ensure that the value of each parameter is determined when the annotation is defined.

Annotated configuration parameters can have default values, and the default value will be used when a configuration parameter is missing.

In addition, most annotations will have a configuration parameter named value . To assign a value to this parameter, you can just write a constant, which is equivalent to omitting the value parameter.

If you only write annotations, it is equivalent to using all default values.

As an example, consider the following code:

java
public class Hello {
    @Check(min=0, max=100, value=55)
    public int n;

    @Check(value=99)
    public int p;

    @Check(99) // @Check(value=99)
    public int x;

    @Check
    public int y;
}

@Check is an annotation. first one @Check(min=0, max=100, value=55) Three parameters are clearly defined. The second @Check(value=99) only defines one value parameter, which is actually exactly the same as @Check(99) . The last @Check means that all parameters use default values.

Summary

Annotation is annotation used by Java language for tool processing:

Annotations can configure parameters, and parameters that do not specify configuration use default values;

If the parameter name is value and there is only one parameter, the parameter name can be omitted.

Use annotations has loaded