Beginners to use SpringBoot commonly used annotations

1. RequestBody and ResponseBody annotations

@RequestMapping("url"), where the url is written as part of the request path, and generally acts on the Controller method, as the request. Insert picture description here
So what about @ResponseBody?

@ResponseBody works on the method. @ResponseBody means that the return result of the method is directly written into the HTTP response body. It is generally used when obtaining data asynchronously [that is, AJAX]. After @RequestMapping is used, the return value is usually parsed as a jump Transfer path, but after adding @ResponseBody, the returned result will not be parsed as a jump path, but written directly into the HTTP response body. For example, after obtaining json data asynchronously, after adding @ResponseBody, the json data will be returned directly. @RequestBody inserts the HTTP request body into the method, and writes the request body into an object using a suitable HttpMessageConverter.

for example:

Front-end asynchronous request: the
Insert picture description here
corresponding method in the back-end Controller class:
Insert picture description here
@RequestBody?

  @RequestBody是作用在形参列表上,用于将前台发送过来固定格式的数据【xml 格式或者 json等】封装为对应的 JavaBean 对象,封装时使用到的一个对象是系统默认配置的 HttpMessageConverter进行解析,然后封装到形参上。

For example, the above login background code can be changed to:
Insert picture description here

2.
@Controller @Controller corresponds to the Bean of the presentation layer, that is, Action. For example,
Insert picture description here
after the UserAction is identified by the @Controller annotation, it means that the UserAction is to be handed over to the Spring container for management. There will be a "userAction" in the Spring container. action, the name is taken based on the UserAction class name. Note: If @Controller does not specify its value [@Controller], the default bean name is the lowercase first letter of the class name of this class. If you specify value [@Controller(value="UserAction")] or [@Controller("UserAction) ")], use value as the name of the bean.

The UserAction here also uses the @Scope annotation. @Scope("prototype") means that the scope of the Action is declared as a prototype. You can use the scope="prototype" of the container to ensure that each request is processed by a separate Action to avoid struts. Thread safety issues in Action. Spring default scope is singleton mode (scope="singleton"), so that only one Action object will be created. Each access is the same Action object, and the data is not safe. Struts2 requires that each access corresponds to a different Action, scope = "Prototype" can ensure that an Action object is created when there is a request
Insert picture description here
3, @ Service

@Service corresponds to a business layer bean, for example:
Insert picture description here
@Service("userService") annotation tells Spring that when Spring wants to create an instance of UserServiceImpl, the name of the bean must be called "userService", so that when Action needs to use UserServiceImpl For instance, you can create "userService" by Spring, and then inject it into Action: In Action, you only need to declare a variable named "userService" to receive "userService" injected by Spring. The specific code is as follows:
Insert picture description here
Note: The type of the "userService" variable declared in the Action must be "UserServiceImpl" or its parent "UserService", otherwise it cannot be injected due to inconsistent types. The "userService" variable declared in the Action is annotated with @Resource Annotate and specify its name = "userService", which is equivalent to telling Spring that my Action wants to instantiate a "userService". You Spring will instantiate it for me, and then give it to me. When Spring sees the userService variable In the @Resource annotation, according to the specified name attribute, you can know that an instance of UserServiceImpl is needed in the Action. At this time, Spring will inject the UserServiceImpl instance created by itself with the name "userService" into the Action. The "userService" variable helps the Action complete the instantiation of the userService, so there is no need to instantiate the userService in the most primitive way of "UserService userService = new UserServiceImpl();" in the Action. If there is no Spring, then when Action needs to use UserServiceImpl, it must pass "UserService userService = new UserServiceImpl(); I have been "reversed". Originally, the initiative is in my hands. I have to use an instance of the "UserServiceImpl" class. I take the initiative to go to the new one and it can be used immediately, but now I cannot take the initiative to go to the new "UserServiceImpl" class instance. , The authority of the new "UserServiceImpl" class instance has been taken away by Spring, only Spring can new the "UserServiceImpl" class instance, and the Action can only wait for Spring to create an instance of the "UserServiceImpl" class before "pleading" Spring Give him an instance of the created "UserServiceImpl" class so that he can use "UserServiceImpl". This is the core idea of ​​Spring "inversion of control", also called "dependency injection". "Dependency injection" is also well understood. Action Need to use UserServiceImpl to work, then it is dependent on UserServiceImpl. Spring injects (ie "gives") the UserServiceImpl that Acion needs to rely on to the Action. This is the so-called "dependency injection". As far as Action is concerned, what the Action relies on requires Spring to inject it. As far as Spring is concerned, what the Action requires, Spring takes the initiative to inject it. I have been "reversed". Originally, the initiative is in my hands. I have to use an instance of the "UserServiceImpl" class. I take the initiative to go to the new one and it can be used immediately, but now I cannot take the initiative to go to the new "UserServiceImpl" class instance. , The authority of the new "UserServiceImpl" class instance has been taken away by Spring, only Spring can new the "UserServiceImpl" class instance, and the Action can only wait for Spring to create an instance of the "UserServiceImpl" class before "pleading" Spring Give him an instance of the created "UserServiceImpl" class so that he can use "UserServiceImpl". This is the core idea of ​​Spring "inversion of control", also called "dependency injection". "Dependency injection" is also well understood. Action Need to use UserServiceImpl to work, then it is dependent on UserServiceImpl. Spring injects (ie "gives") the UserServiceImpl that Acion needs to rely on to the Action. This is the so-called "dependency injection". As far as Action is concerned, what the Action relies on requires Spring to inject it. As far as Spring is concerned, what the Action requires, Spring takes the initiative to inject it. ceImpl works, then it is dependent on UserServiceImpl. Spring injects (that is, "gives") the UserServiceImpl that Acion needs to depend on to the Action, which is the so-called "dependency injection". As far as Action is concerned, what the Action relies on requires Spring to inject it. As far as Spring is concerned, what the Action requires, Spring takes the initiative to inject it. ceImpl works, then it is dependent on UserServiceImpl. Spring injects (that is, "gives") the UserServiceImpl that Acion needs to depend on to the Action, which is the so-called "dependency injection". As far as Action is concerned, what the Action relies on requires Spring to inject it. As far as Spring is concerned, what the Action requires, Spring takes the initiative to inject it.

4.
@Repository @Repository corresponds to the data access layer Bean, for example:
Insert picture description here
@Repository(value="userDao") Annotation tells Spring to let Spring create an instance of UserDaoImpl named "userDao".

When the Service needs to use the UserDaoImpl instance named "userDao" created by Spring, you can use the @Resource(name = "userDao") annotation to tell Spring that Spring can inject the created userDao into the Service.

Insert picture description here

5.@Autowired

@Autowired(required=true): When using @Autowired annotation, in fact, the default is @Autowired(required=true), which means that the bean must exist during injection, otherwise the injection will fail.
@Autowired(required=false): Indicates to ignore the bean currently to be injected. If there is direct injection, there will be no skip and no error will be reported.

6.
@ComponentScan uses the @ComponentScan annotation in the springboot configuration class or startup class
(function: scans all interfaces in the specified package, which is equivalent to writing @Service or @Component or @Repository or @Controller on each interface)
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44941105/article/details/95737667