Commonly used Java annotations in detail

Java is a commonly used programming language, and annotations are a very important part of the Java language. In this article, we will introduce some commonly used Java annotations, their functions and how to use them.

@Override
The @override annotation is used to indicate that a method is overridden. In Java, if the subclass wants to override the method of the parent class, the method of the subclass must be consistent with the method name, parameters, and return value type of the parent class. If the method in the subclass is inconsistent with the method in the parent class, the compiler will report an error. And using the @override annotation allows the compiler to check whether the method of the parent class is correctly overridden.

@Deprecated
The @deprecated annotation is used to indicate that a class, method or field is obsolete. If you use the @deprecated annotation on a deprecated class, method, or field, the compiler will give you a warning, prompting you to use other alternatives.

@SuppressWarnings
The @SuppressWarnings annotation is used to turn off warning messages from the Java compiler. In some cases, the compiler will give you some meaningless warning messages, and you can use the @SuppressWarnings annotation to turn off these warning messages.

@SafeVarargs
The @SafeVarargs annotation is used to indicate that a method is a safe variable parameter method. In Java, variadic methods will give you some warning messages, because it may lead to type unsafety. However, in some cases, you can use the @SafeVarargs annotation to indicate that your variable parameter method is safe, thereby turning off these warning messages.

@FunctionalInterface
The @FunctionalInterface annotation is used to indicate that an interface is a functional interface. In Java, a functional interface is an interface with only one abstract method. If you use the @FunctionalInterface annotation on an interface, the compiler will check whether the interface has only one abstract method. If this interface has more than one abstract method, the compiler will report an error.

@SuppressWarnings
The @SuppressWarnings annotation is used to turn off warning messages from the Java compiler. In some cases, the compiler will give you some meaningless warning messages, and you can use the @SuppressWarnings annotation to turn off these warning messages.

@Retention
The @Retention annotation is used to indicate the life cycle of an annotation. In Java, there are three annotation life cycles: source level (@Retention(RetentionPolicy.SOURCE)), compile-time level (@Retention(RetentionPolicy.CLASS)) and runtime level (@Retention(RetentionPolicy.RUNTIME)). If you don't specify the lifetime of annotation, it defaults to source level.

@Documented
The @Documented annotation is used to indicate whether an annotation needs to be included in the JavaDoc document. If you use the @Documented annotation on an annotation, the JavaDoc documentation will include a description of the annotation.

@Inherited
@Inherited indicates whether an annotation can be inherited. In Java, annotations are not inherited by default, but if you use the @Inherited annotation on an annotation, it can be inherited.

@Resource
The @Resource annotation is used to indicate that a class or method needs to inject a dependency. In Java, Dependency Injection is a very common technique that helps you manage relationships between objects. If you use the @Resource annotation on a class or method, the container will automatically inject a dependency.

@PostConstruct
The @PostConstruct annotation is used to indicate that a method is invoked immediately after dependency injection. In Java, sometimes you need to perform some initialization operations after object creation, then you can use the @PostConstruct annotation to mark these operations.

@PreDestroy
The @PreDestroy annotation is used to indicate that a method is called before the object is destroyed. In Java, sometimes you need to perform some cleanup operations before the object is destroyed, then you can use the @PreDestroy annotation to mark these operations.

@Transactional
The @Transactional annotation is used to indicate that a method needs to be run in a transaction. In Java, transaction is a very important concept that can help you ensure the consistency of database operations. If you use the @Transactional annotation on a method, the container will automatically open a transaction, and then commit the transaction after the method is executed.

@Controller
The @Controller annotation is used to indicate that a class is a controller. In Java, controllers are used to handle HTTP requests, and they usually return a view. If you use the @Controller annotation on a class, the container will automatically register it as a controller.

@Service
The @Service annotation is used to indicate that a class is a service layer. In Java, the service layer is usually used to implement business logic. If you use the @Service annotation on a class, the container will automatically register it as a service.

@Repository
The @Repository annotation is used to indicate that a class is a data access layer. In Java, the data access layer is usually used to operate the database. If you use the @Repository annotation on a class, the container will automatically register it as a data access layer.

Summarize

Java annotations are a very important feature that help you mark up your code and provide some additional information to the container. In this article, we introduced some commonly used Java annotations, which are:

@Override
@Deprecated
@SuppressWarnings
@SafeVarargs
@FunctionalInterface
@Retention
@Documented
@Inherited
@Resource
@PostConstruct
@PreDestroy
@Transactional
@Controller
@Service
@Repository
Hope this article can help you better understand Java annotations and correctly write Java code use them.

Guess you like

Origin blog.csdn.net/weixin_51151534/article/details/129461411