Development tools - Common notes

Java annotation, also known as Java annotation, is jdk5 0 is an annotation mechanism introduced by.
Classes, methods, variables, parameters and packages in Java language can be annotated. Java annotation can obtain the annotation content through reflection. When the compiler generates class files, annotations can be embedded in bytecode. The Java virtual machine can retain the annotation content and obtain the annotation content at run time. Of course, it also supports custom Java annotations

I Some common annotations of Spring

1. Annotation of declared bean

@Component: generally refers to various components without clear roles
@Service is used in the business logic layer (service layer)
@Repository is used in the data access layer (dao layer)
@The Controller is used in the presentation layer, and the Controller declaration (Controller)
@Override - checks whether the method is an overridden method. If it is found that its parent class or the referenced interface does not have this method, a compilation error will be reported.

give an example:

package cn.tedu.ioc;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

@Component("a")//Automatically complete the ioc, and specify the bean name a - > {"a", which is provided by new User()}spring for ioc
//@Component / / the name of the default bean, user
//@Component autocomplete IOC - > {"user", new User()}
//@Provided by Controller//spring for ioc
//@Provided by Service//spring for ioc
public class User {
    public void get(){

2. Annotation of injected bean

@Autowired: it comes with the spring framework and is automatically assembled as the name suggests. Spring will automatically assemble the elements we mark as @ Autowired, and implement attribute injection of custom bean type through @ Autowired on the attributes in the bean. When using, the corresponding bean must be managed by spring, that is, manually configure the bean or add @ Component annotation on the class.

package cn.tedu.di2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

public class Student {
    @Autowired//The bottom layer is reflection. When the framework is completed: new student() setTeacher(new Teacher());
    Teacher t ;
    String name = "Cai Xukun";

    public String toString() {
        return "Student{" +
                "t=" + t +
                ", name='" + name + '\'' +

@Inject: provided by JSR-330, @ inject is used in the same way as @ Autowired.

@Resource: provided by JSR-250

@Autowired and @ Inject are matched by type by default, and @ Resource is matched by Name. If @ Autowired needs to be matched by Name, it needs to be used with @ Qualifier, and @ Inject and @ Name.

@Primary: when spring performs automatic assembly, the preferred bean and @ Qualifier are used by default. configuration class related notes

@Configuration: declare the current class as a configuration class, in which the @ Component annotation is internally combined, which is equivalent to Spring configuration in xml form.

4. Section AOP related notes

@Aspect declares an aspect (on a class)
@After is executed after the method is executed, and post notification (on the method)
@Before execution before method execution, pre notification (on method)
@Around executes before and after the method execution, and surrounds the notification (on the method)

@PointCut declaration tangent point

5.@Value note: there are several ways to assign values to attributes:

1. Inject ordinary characters
String name;
2. Inject operating system attributes
String osName;
3. Inject expression results
@Value("#{ T(java.lang.Math).random() * 100 }") 
String randomNumber;
4. Inject other bean attributes
String name;
5. Inject file resources
String Resource file;
6. Inject website resources
Resource url;
7. Injection profile
String bookName;

@Value is used in three cases:

${} is to find the parameter of external configuration and assign the value
#{} is a spiel expression to find the content of the corresponding variable
#To write a string directly is to inject the value of the string into it

6. Attribute support of @ bean

II Spring MVC section

@Controller: declare this class as the controller in spring MVC

@RequestMapping: used to map Web requests, including access paths and parameters (on classes or methods)

@ResponseBody: it supports putting the return value in the response instead of a page. Usually, the user returns json data (next to the return value or on the method)

@RequestBody: the parameters of the request are allowed to be in the request body, rather than directly connected after the address. (put in front of parameters)

@RestController: this annotation is a combined annotation, which is equivalent to the combination of @ Controller and @ ResponseBody. The annotation is on the class, which means that @ ResponseBody is added to all methods of the Controller by default.

@CrossOrigin / / request to release JS - cross domain solution

III lombok annotation

Data will automatically generate set get toString equals hashCode
@NoArgsConstructor automatically generates parameterless constructs
@AllArgsConstructor automatically generates all parameter constructs
@Accessories (chain = true) / / enable chain programming

Keywords: IDE

Added by ZaphodQB on Mon, 20 Dec 2021 04:24:38 +0200