Summary of Type Knowledge Points in java Annotations

  • 2021-09-05 00:04:00
  • OfStack

When it comes to annotations in java, there is still a big difference with our usual annotations, which are mainly used as java features, and are used at the same level as our common classes. As for the types of java annotations, we can simply divide them into custom annotations and meta-annotations. Among them, JDK in meta-annotation has 5 annotation types. Let's take a look at the specific content explanation in the following 1.

1. Custom annotations

Keyword used to define annotations: @ interface


// #1  Definition annotation 
public @interface MyAnno1{
}

2. Meta-annotation

A comment used to modify a comment.

Five meta-annotations provided by JDK:

(1) @ Target: Used to determine where to use custom annotations to be decorated

(2) @ Retention: Used to determine the decorated custom annotation life cycle

(3) @ Inherited: Indicates that the annotation is inherited (understand)

(4) @ Documented: Do you include this annotation when generating api documents using javadoc (understand)

(5) @ Repeatable: The annotation can only appear once in the same position. With @ Repeatable, you can use it many times in the same place.


package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import anno.JDBCConfig;

@JDBCConfig(ip = "127.0.0.1", database = "test", encoding = "UTF-8", loginName = "root", password = "admin")
@JDBCConfig(ip = "127.0.0.1", database = "test", encoding = "UTF-8", loginName = "root", password = "admin")

public class DBUtil {
 static {
   try {
     Class.forName("com.mysql.jdbc.Driver");
   } catch (ClassNotFoundException e) {
     e.printStackTrace();
   }
 }

 public static Connection getConnection() throws SQLException, NoSuchMethodException, SecurityException {
   JDBCConfig config = DBUtil.class.getAnnotation(JDBCConfig.class);
   System.out.println(config);

   String ip = config.ip();
   int port = config.port();
   String database = config.database();
   String encoding = config.encoding();
   String loginName = config.loginName();
   String password = config.password();

   String url = String.format("jdbc:mysql://%s:%d/%s?characterEncoding=%s", ip, port, database, encoding);
   return DriverManager.getConnection(url, loginName, password);
 }

 public static void main(String[] args) throws NoSuchMethodException, SecurityException, SQLException {
   Connection c = getConnection();
   System.out.println(c);
 }

}

Extension of knowledge points:

Understanding Java annotations

In fact, Java annotations are not much different from ordinary modifiers (public, static, void, etc.). The following examples are common annotations:


public class AnnotationDemo {
 //@Test Annotation modification method A
 @Test
 public static void A(){
  System.out.println("Test.....");
 }

 //1 You can have multiple different annotations on each method 
 @Deprecated
 @SuppressWarnings("uncheck")
 public static void B(){

 }
}

After using @ Test annotation on the method, when running the method, the test framework will automatically recognize the method and call it separately. @ Test is actually a kind of tag annotation, which acts as a tag and tells the test framework that the method is a test method at runtime. For @ Deprecated and @ SuppressWarnings ("uncheck"), they are built-in annotations of Java itself, which can be often seen in the code, but this is not a good thing. After all, when there is @ Deprecated annotation on a method or class, it means that the method or class has expired and is not recommended to be used again. @ SuppressWarnings means ignoring specified warnings, such as @ SuppressWarnings ("uncheck"), which is the simplest way to use annotations


Related articles: