Detailed explanation of Java annotation and custom annotation
1: Introduction to Java annotations
Annotations are often used in development, and custom annotations are occasionally seen in projects. Today, let's discuss what the annotation is, the application scenario of the annotation and how to customize the annotation.
The following is a list of common annotations in development
@Override: used to identify that the method inherits from the superclass. When the method of the parent class is deleted or modified, the compiler will prompt an error message (we can always see this on the toString () method we most often see)
@Deprecated: indicates that the class or method has been deprecated and expired. If the user still wants to use it, a compilation warning will be generated
@Suppresswarnings: compiler warnings to ignore
JUnit test: @ test
Some notes of spring: @ controller, @ requestmapping, @ requestparam, @ ResponseBody, @ service, @ component, @ repository, @ resource, @ autowire
Comments for Java validation: @ notnull, @ email
Let's take a look at the annotation override Lushan true face of Java
2: Java annotation Basics
1. Java annotation data type
The notes are written in In java files, @ interface is used as the keyword, so annotation is also a data type of Java. From a broad definition, class, interface, enum and annotation all belong to class type.
2. Java meta annotation
When creating annotations, you need to use some annotations to describe the annotations you created, that is, the annotations written on @ interface. These annotations are called meta annotations, such as @ target, @ retention, etc. seen in override. Some meta annotations are listed below
@Documented: used to mark whether to include annotations when generating Javadoc. You can see that like @ override, the annotation is empty and has nothing
@Target: used to define where annotations can be used. By default, annotations can be used anywhere. You can also specify the scope of use. Annotations are commonly used on classes (such as @ controller), fields (such as @ autowire), methods (such as @ requestmapping) and method parameters (such as @ requestparam) in development.
Target. java
@Inherited: allows subclasses to inherit annotations from the parent class. Annotations from the parent class can be obtained through reflection
@Constraint: used to verify whether the attribute value is legal
@Retention: the annotation declaration cycle, which is used to define the annotation survival stage. It can survive at the source code level, compilation level (bytecode level) and runtime level
Source: source level. Annotations exist only in the source code. They are generally used to interact with the compiler and detect code. For example, @ override, @ suppresswarings.
Class: bytecode level. Annotations exist in source code and bytecode files. They are mainly used to generate additional files during compilation, such as XML and java files, but they cannot be obtained at runtime. For example, mybatis generates entity and mapping files. At this level, you need to add a Java agent when loading the JVM, and use the agent to dynamically modify the bytecode file.
Runtime: runtime level. Annotations exist in source code, bytecode and Java virtual machine. They are mainly used for runtime. Reflection can be used to obtain relevant information.
3. Content of Java annotation
In the above annotation source code, you can see that some annotations do not have any content, and some annotations have content, which looks like a method.
Syntax format of annotation content: data type attribute name () default default value. Data type is used to describe the data type of attribute. The default value means that the default value is used when no value is assigned to the attribute. Generally, string uses empty string as the default value, and array generally uses empty array {} as the default value
Let's take a look at the declaration of the request mapping annotation in spring MVC
Use the requestmapping annotation in spring MVC
4. Usage scenario of annotation
The usage scenarios of annotations can be analyzed through the declaration cycle of annotations:
Source source level: for the compiler, such as @ override, @ deprecated, etc. there are few scenarios that developers should use
Class: bytecode level, which is rarely seen
Runtime: runtime level, which is the most. Almost all annotations used by developers are runtime level. Runtime annotations are commonly used in the following cases
The annotation is empty without any attribute. This part of the annotation usually plays the role of a annotation, such as @ test, @ before, @ after. Some special processing is done logically by obtaining these tag annotations
You can use the constraint annotation @ constraint to verify the attribute value, such as @ email, @ notnull, etc
You can configure some parameters by using attributes in annotations, and then use reflection to obtain these parameters. These annotations have no other special functions, but simply configure some parameters instead of XML configuration. Use annotations to configure parameters, which is popular in spring boot, such as @ configuration
As for the configuration method XML vs annotation, XML is generally used to configure some configurations that are not closely related to the business, and annotations are used to configure some parameters closely related to the business.
3: Java annotation and reflection basic API
4: Custom annotation
Permission can be controlled by using custom annotation + interceptor or AOP.
The following example is used to restrict the user to log in when accessing the interface by defining an annotation
Step 1: define annotations
RequiresLogin. java
Step 2: use annotations
Step 3: use AOP to intercept and parse annotations
In ApplicationContext Configuring AOP in XML
Custom exception
Why customize exceptions
Although Java provides rich exception handling classes, custom exceptions are often used in projects. The main reason is that the exception classes provided by java can not meet the needs of various businesses in some cases. For example, some errors in the system conform to Java syntax, but do not conform to business logic. If the account does not exist or the account is locked when the user logs in, you can customize an account exception accountexception.
Or in some cases, the same exception in Java may be caused by multiple reasons. It is not easy to locate the error when troubleshooting the problem. At this time, you can customize a more specific exception.
Benefits of custom exceptions: custom exceptions can make exceptions clearer and hide the underlying exceptions, which is safer and more intuitive.
Use of custom exceptions: custom exceptions are generally inherited from exception or runtimeException. According to business needs, some attributes can be taken as constructor parameters. Custom exceptions require programmers to throw exceptions manually and handle exceptions.
The following is an example of a custom exception in Apache Shiro
The above is a detailed description of Java annotation and custom annotation