Summary of Commonly Used Annotations for Spring Framework Learning

  • 2021-11-24 01:27:13
  • OfStack

Catalog class annotation annotation on method or property annotation parameter annotation

Class annotation

@ component annotation class generally refers to various components. When the class does not belong to various classifications, it is used as annotation.
@ Service annotates the class, declaring it as a business layer component for processing business logic

@ Repositor annotates the class, declaring it an interface to the persistence layer. After use, you need to add the @ MapperScan ("xxx. xxx. xxx. mapper") annotation on the startup main program class
The @ Mapper annotation class is used on the interface of the persistence layer. After the annotation is used, it is equivalent to @ Reponsitory plus @ MapperScan annotation, and the configuration will be loaded automatically

@ Configuration Spring 3.0 or above, declaring that this class is a configuration class, and you can use @ Configuration to define the configuration class, which can replace the xml configuration file. The annotated class contains one or more methods annotated by @ Bean.

The @ Aspect annotation class declares that this class is a facet class

@ Controller annotates the class, declaring it an Spring MVC controller processor component for creating objects that process http requests.
@ RestController annotates the class, declaring that the class is an Rest style controller component. The annotation is added after Spring4. If you replace @ Controller with it, you don't need to configure @ ResponseBody again. By default, it returns json format

@ RequestMapping: It can be annotated either on the class or on the methods of the class, which provides preliminary request mapping information. The annotation is relative to the Web root directory on the class, and the annotation is relative to the path on the class on the method


@Controller
@RequestMapping("/user")
public class UserController {
 	@RequestMapping("/login")
	public String login() {
		return "success";
	}

At this point, the call is made using: http://IP Address: Port Number/Website RootPath/user/login

Annotation on method or property

@ Autowired is used to assemble bean, which can be written on fields or methods. Dependent objects must be required to exist by default, and to allow null values, you can set its required property to false, for example: @ Autowired (required=false)
@ Qualifier If an interface has two or more implementation classes, the @ Qualifier annotation should be used. The English meaning of qualifier is the meaning of qualified person. Through this annotation, mark which implementation class is the implementation class to be used this time. Such as:


@Service("service")
public class EmployeeServiceImpl implements EmployeeService {
    public EmployeeDto getEmployeeById(Long id) {
        return new EmployeeDto();
    }
}
@Service("service1")
public class EmployeeServiceImpl1 implements EmployeeService {
    public EmployeeDto getEmployeeById(Long id) {
        return new EmployeeDto();
    }
}

When service and service1 implement interface EmployeeService and @ Autowired injection at the same time, tell spring which implementation class you want through @ Qualifier. The code is as follows


@Autowired
@Qualifier("service")
EmployeeService employeeService;

This is service, not service1.

The @ Bean works with the @ Configuration annotation class and is equivalent to the bean configured by the xml file. Such as:


<bean id="user" class="com.zhang.bean.User">
     <property name="userName" value="zhangsan"></property>
     <property name="age" value="26"></property>
</bean>

Equivalent to


 @Bean
    public User getUser(){
        User user = new User();
        user.setUserName ( "zhangsan"),
        user.setAge ( 26),
        return user;
    }

@ After, @ Before, @ Around: Used in conjunction with @ Aspect to take the tangent point directly as a parameter and execute after, before, and before and after the method is executed.
@ RequestBody: Can be used on methods or parameters. On the method, annotations return json data on behalf of users, not pages.

Parameter annotation

@ RequestBody: Annotation on the parameters of the method, which means that the received parameters are from requestBody, that is, the request body. It is used to process data in non-Content-Type: application/x-www-form-urlencoded encoding format, such as application/json, application/xml, etc. All json data in body can be transferred to the back end by using annotation @ RequestBody, and then the back end can analyze it

@ RequestParam: Used on method parameter parameters, which are received from the HTTP request body or QueryString requesting url. You can accept properties of simple types or object types. @ RequestParam is used to process Content-Type for application/x-www-form-urlencoded encoded content, and Content-Type defaults to this attribute.

@ PathVariable: Used on method parameter parameters. When @ RequestMapping URI template style maps, paramId can bind its passed-in values to the parameters of the method through the @ Pathvariable annotation, such as:


@Controller
@RequestMapping("/user/{Id}")
public class DemoController {
  @RequestMapping("/pets/{petId}")
  public void queryPetByParam(@PathVariable String Id,@PathVariable String petId) {    
    // implementation
  }
}

The above is the Spring framework learning common annotation summary details, more information about Spring framework annotation please pay attention to other related articles on this site!


Related articles: