Common annotations and examples of springboot

一、 @SpringBootApplication

Among SpringBootthe startup classes automatically created by the framework for us, @SpringBootApplicationannotation tags are used to modify the startup class.
@SpringBootApplicationThe annotation tag is actually a compound annotation tag, including @EnableAutoConfiguration, @ComponentScanusually used on the main class

annotation illustrate
@SpringBootApplication Used to declare startup classes for modification
@EnableAutoConfiguration It is springbootthe core annotation for automatic configuration, through which springthe application needs to be beaninjected into the container
@ComponentScan It is used to automatically scan the classes identified by annotations and generate them iocin the container bean. The default scanning scope is @ComponentScanthe classes in the configuration class package and sub-packages where the annotations are located

Two, @Configuration, @Bean

annotation illustrate
@Configuration Act on the class to configure springthe container (application context), which is equivalent to using the class as a springconfiguration xmlfile
@Bean Create beanan object and add it to the container, acting on the method
Example:

SSMDuring the integration of the native framework, if we want to IOCadd a custom object to the container, what we need to do is to add the content declaration of the following tag
under Springthe heel tag of the configuration file used by the framework:

<bean id="user" class="com.qf.testspringboot.pojo.User">
 <property name="id" value="1"/>
 <property name="username" value="张三"/>
 <property name="age" value="22"/>
</bean>

But in SpringBootthe framework, we are allowed to directly IOCinject custom objects into the container by creating and returning Java objects.
This process depends on @Configurationannotation tags and @Beanannotation tags.
First we prepare a Userclass:

@Data //记得要导入Lombok相关的依赖
@NoArgsConstructor
@AllArgsConstructor
public class User {
    
    
 private Integer id;
 private String username;
 private Integer age;
}

Then create a UserConfigurationclass and use @Configurationthe annotation tag for this class to declare that this class is a configuration class:

@Configuration
public class UserConfiguration {
    
    
}

Create a method in this class, the return value of the method is Usertype, and the method name is IOCthe object name of the injected object in the container, and use @Beanthe annotation label for this method:

@Configuration
public class UserConfiguration {
    
    
  @Bean
  public User user() {
    
    
  /*
  下列代码等价于:
  <bean id="user" class="com.qf.testspringboot.pojo.User">
  <property name="id" value="1"/>
  <property name="username" value="张三"/>
  <property name="age" value="22"/>
  </bean>
  方法的返回值数据类型等价于<bean>标签中的class属性取值
  方法的方法名等价于<bean>标签中的id属性取值
  */
  User user = new User(1, "张三", 22);
    return user;
  }
}

At this point, the logic flow for SpringBootthe framework to scan and IOCinject objects into the container is:

  1. When the SpringBoot framework scans @Configurationthe type decorated with annotation tags, it will consider this class as a configuration class;
  2. @BeanThe return value object of the method decorated with annotation tags in the configuration class will be automatically injected IOCinto the container for storage;
  3. @BeanThe data type of the return value of the annotation tag decoration method is equivalent to the attribute value of the tag class;
  4. By default, @Beanthe method name of the method decorated with the annotation tag is equivalent to idthe attribute value of the tag, that is, the IOCobject name of the object in the container.

In this way, we can XMLeasily IOCinject custom Java objects into the container without using configuration files.


3. @AutoWired

@AutowiredBy default, it is assembled by type, which is often used in business layer implementation classes and persistence layer implementation classes

Example:

In TestControllerthe class, @Autowiredan object is automatically injected through the annotation tag User, and when testthe method is accessed, the Userobject is printed:

@Controller //SpringMVC当中的注解标签都还能用
public class TestController {
    
    
 @Autowired
 private User user; //自动注入自定义的User对象
 @RequestMapping("/test")
 @ResponseBody
 public String test() {
    
    
 System.out.println(user); //打印这个User对象
 return "Hello SpringBoot!";
 }
}

The running result of the program when accessing testthe method:
insert image description here


四、@Service、@Controller、@Repository、@Component

annotation illustrate
@Service For the business layer, the business logic layer serviceis injected into springthe container
@Controller control layer controllerinjection springcontainer
@Repository Persistence layer daoinjection springcontainer
@Component common domaininjection springcontainer

五、@ResponseBody、@RestController

annotation illustrate
@ResponseBody Act on the method or class, let the return result of the method be directly written into HTTP response body, without going through the view parser, and the returned data will be displayed directly on the page.
@RestController It is a combination of @Controllerand , acting on the class, the effect is equal to adding and @ResponseBodyon the class@ResponseBody@Controller

六、@RequestMapping、@GetMapping、@PostMapping

annotation illustrate
@RequestMapping urlUse this annotation to access through configuration , which can getbe or post
@GetMapping Use this annotation to urlaccess through configuration and limit getthe request method
@PostMapping Use this annotation to urlaccess through configuration and limit postthe request method

七、@RequestParam、@RequestBody

annotation illustrate
@RequestParam It is mainly used to receive url?the following parameters, getor postrequests, as long as there are parameters behind, url?the corresponding parameters can be obtained
@RequestBody This annotation is used to obtain request body data ( body), getthere is no request body, so it is generally used for postrequests
  1. In GETrequests, you cannot use@RequestBody
  2. In the request, and POSTcan be used , but if used , the configuration of parameter conversion must be unified.@RequestBody@RequestParam@RequestBody
  3. Can use multiple @RequestParamfetch data, @RequestBodynot

Guess you like

Origin blog.csdn.net/klylove/article/details/125408327