1. Meta-annotations (annotations used to modify annotations)
Second, annotations in Spring
- For SpringMVC annotations
- IOC container notes
- Comments on the scope of the Bean:
- Bean lifecycle notes:
- Spring startup class annotations (out of the box) :
- Request Mapping annotation
- Dynamic assignment annotation
- Cache annotations
Third, AOP annotations
4. Common plug-in annotations
- Lombok plug-in
- MybatisPlus annotations
- Shiro annotations
Other notes
- Async Asynchronous annotations
- Annotation annotations
- Other annotations
First, let’s talk about annotations:
Annotations are simply special tags in code that are used in place of configuration files, and with annotations, developers can tell classes how to behave. Use meta-annotations to define (modify) custom annotations and define the functionality that needs to be implemented.
Annotations can be marked on packages, classes, properties, methods, method parameters, and local variables, and multiple annotations can be marked in the same place. A typical use of annotations in Java technology is that you can use reflection to retrieve annotations inside a class to determine how to run the class.
1. Meta-annotations (annotations used to modify annotations)
Since JDK 1.5, Java has added MetaData support, providing four standard annotation classes for annotating annotation types, We call this meta-annotation. @target (ElementType.) describes the scope of the annotation (where the modified annotation can be used)
The values (ElementType) are: LOCAL_VARIABLE used to describe local variables 4.METHOD used to describe methods 5.PACKAGE Used to describe packages 6.PARAMETER Used to describe parameters 7.TYPE: Used to describe classes, interfaces (including annotation types), or enum declarations
@Retention(RetentionPolicy.) describes the annotation life cycle (i.e., how long the modified annotations are retained)
The RetentionPoicy values are as follows: 1.SOURCE: valid in the SOURCE file (reserved in the SOURCE file) 2.CLASS: valid in the CLASS file (reserved in the CLASS file) 3.
Documented @Documented is extracted dynamically by the Javadoc tool. Inherited Allows a child class to inherit annotations from its parent.
Second, annotations in Spring
For SpringMVC annotations
The classes described by these annotations create native or proxy objects that Spring hands over to the IOC container to manage, called beans. When used, @autoWired can be injected directly.
@Mapper Describes the data layer (Mapper) @Service Describes the business layer (Service) @Repository identifies the persistence layer/data access layer Component (Dao) @Component can describe various components (when components are hard to categorize) RestController describes the control layer (Controller) and returns the JSON data type, but no longer executes the view parser in SpringMVC. /* This annotation is equivalent to: @controller + @responseBody */ @controller describes how the Controller layer receives the user request to execute the view resolver (path concatenation prefix + suffix ViewResolver parsing the view rendering). ResponseBody Converts Java objects to json format data.
IOC container notes
IOC is an Inversion of Control, also called dependency injection. The decomposition of complex systems into cooperative objects, whose internal implementation is transparent to the outside through encapsulation, reduces the complexity of problem solving, and can be flexibly reused and extended. Simply put: IOC means handing your designed objects over to the container for control, injecting (retrieving) them through annotations as needed, rather than the traditional direct control within your objects (new objects). Thus reducing the coupling of the program.
The @bean describes the return value of the method to be managed by the container without having to call the method manually. @autoWired injected objects (automatically injected by byType) @Resource injected objects (automatically injected by byName) @Value injected ordinary type properties.
If the container have more the same type of bean, the framework will throw NoUniqueBeanDefinitionException, to prompt more satisfy the conditions of beans for automatic assembly. When the program cannot correctly decide which one to use, it can use the following annotation 👇
@Qualifier(“”), after being named on the same type of bean, can be injected with a different name in conjunction with @AutoWired. @primary When there are more than one bean of the same type, the bean annotated by @primary is preferred.
Annotation of the Scope of the Bean: @scope (value=””) The default generated class is singleton.
Prototype: multiple instances 3. Request: Request domain, which is required in the Web environment 4. Session: Session domain, which is required in the Web environment 5. Context domain: in the Web environment 6. Globalsession Session domain of the cluster environment: in the Web environment
Note: @postconstruct equals init-method. @predestroy equals destroy-method
Spring startup class annotations (out of the box) :
You only need to import a simple JAR package file to implement the corresponding functions without (a little) tedious configuration. @springBootAppliction is used on startup classes to enable automatic configuration combinations:
1) @springBootConfiguration defines the SpringBootConfiguration class. @Configuration defines the Configuration information through @configuration. The @AutoConfigurationPackage automatically configures the package scan. @import calls the selector to load the startup items in the POM.xml file.
RequestMapping @requestMapping (“/ XXX “) Specifies which URL requests the controller can handle with a “/ XXX “annotation class.
Request method:
@getMapping usually annotates query methods @postMapping usually annotates add save methods @deletemapping usually annotates delete methods @putMapping usually annotates update methods @PatchMapping usually annotates update local methods
Dynamic assignment annotation
@pathVariable Receives url that is dynamically passed to annotated parameters (restFull style) @RequestBody Converts received JSON-formatted data to Java object parameters (for POST requests) @RequestParam(value= “received XXX”) Pass the received XXX to the annotated parameter (for POST, GET requests)
Cache annotations
EnableCaching Starts the built-in cache in the Springboot project. @cacheable (value= “Cacheable name”) Cacheable(value= “Cacheable name”) Cacheable(value= “Cacheable name”) Cacheable(value= “Cacheable name”) Cacheable(value= “Cacheable name”)
parameter | describe |
---|---|
value | The name of the |
key | key |
condition | You can determine key conditions |
The @cacheevict (value= “Cache name to flush”) method is a pointcut method that cleans the cache when called.
parameter | describe |
---|---|
value | The name of the |
key | key |
condition | The condition of the cache can be null |
allEntries | Whether to clear all cached contents |
beforeInvocation | Whether the method is cleared before execution |
Third, AOP annotations
@pointcut (“@annotation(address of cut method)”) sets the Pointcut @before (” Pointcut()”) to be executed Before the Pointcut method @after (” Pointcut()”) to be executed After the Pointcut method The @around (” pointCut() “) method is executed Around the pointCut method, and the ProceedingJoinPoint object’s PROCEED method is executed to load the method to be cut in.
4. Common plug-in annotations
Lombok plug-in
@ Data dynamically add get/set/toString/equals/hashcode/construction method is suitable for the pojo/VO this annotation is equal to:
@Getter + @Setter + ToString + @EqualsAndHashCode + @RequiredArgsConstructor
@value sets all variables to final similar to @data @allargsconstructor adds constructor @noargsconstructor adds no-argument constructor @sfl4g automatically generates log static constants for this class Accessors(chain = true) references the chain loading mode to facilitate insertion operations.
Lombok plug-in
projectlombok.org/
MybatisPlus annotations
MyBatis-Plus (Opens New Window) (MP) is a new enhancement tool for MyBatis (Opens New Window), which is designed to simplify development and improve efficiency.
@tablename (value=””, resultMap=””) if the TableName is inconsistent with the entity class name, add a annotation “value= TableName “to the entity class. If the resultMap ids in XML are inconsistent, assign a value. @tableId (value= “”, type = idtype.auto) indicates the primary key name/attribute. The values of @idType are:
AUTO Database autoincrement INPUT INPUT ID_WORKER Unique DISTRIBUTED global ID Long integer type UUID 32-bit UUID string NONE Stateless ID_WORKER_STR Unique distributed global ID A string of characters
@ TableField (“…” If the field name is the same as the attribute (the hump rule is enabled), the field name can be omitted; otherwise, “exist=false” can be added. V
parameter | describe |
---|---|
value | Field value. If the field name is the same as the attribute (the hump rule is enabled), you can omit it |
update | Preprocessing set field custom injection |
condition | Preprocessing WHERE entity conditions custom arithmetic rules |
exist | Whether it is a database table field |
fill | The fields |
@TableLogicTable field logic handling annotations (logical deletion) |
Mybatis-Plus
Mp.baomidou.com/guide/annot…
Shiro annotations
Shiro provides annotations for permission control, and using these annotations requires using AOP’s capabilities for judgment
RequiresAuthentication @requiresauthentication indicates that the current Subject isAuthenticated by login, that is, subject-.isauthenticated () returns true. RequiresGuest @requiresGuest indicates that the Subject is not currently authenticated or is a tourist by remembering that I logged in. RequiresUser @requiresuser indicates that the current Subject has been authenticated or by remembering that I logged in. @requiresroles (value={” admin “, “user”}, logical= logical. AND) specifies that the current Subject needs roles admin AND (AND) user, AND value specifies the role to be determined. Logical indicates a judgment condition. RequiresPermissions(value={” user:a “, “user:b”}, logical= logical. OR) specifies that the current Subject RequiresPermissions user:a OR (OR) user:b, Value indicates the permission to be determined, and logical indicates the judgment condition.
Shiro framework
shiro.apache.org/#
Other notes
Async Asynchronous annotations
The @async annotation describes a method as an asynchronous pointcut method (which declares that the method performs asynchrony) and requires @enableAsync on the startup class for it to take effect. This method is executed through a new thread call in the aspect notification method, provided by the Spring thread pool. @enableAsync Can be described using multiple threads to support asynchrony
Annotation annotations
@param Dao layer (Mapper) annotation, used to pass multiple arguments @return indicates that the method has a return value. It’s just an illustration
Other annotations
Different service files are stored in different yML configuration files, so dynamic loading of the configuration file @propertysource (value= “classpath:/…”) is required. ,ebcidubg= “UTF-8”) dynamically loads the configuration file in order to assign a value to the defined variable @select (“… “) A simple SQL statement can use this annotation to describe the Controller described by @crossorigin directly methodically, allowing cross-domain access
end