一、 @SpringBootApplication
Among SpringBoot
the startup classes automatically created by the framework for us, @SpringBootApplication
annotation tags are used to modify the startup class.
@SpringBootApplication
The annotation tag is actually a compound annotation tag, including @EnableAutoConfiguration
, @ComponentScan
usually used on the main class
annotation | illustrate |
---|---|
@SpringBootApplication | Used to declare startup classes for modification |
@EnableAutoConfiguration | It is springboot the core annotation for automatic configuration, through which spring the application needs to be bean injected into the container |
@ComponentScan | It is used to automatically scan the classes identified by annotations and generate them ioc in the container bean . The default scanning scope is @ComponentScan the 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 spring the container (application context), which is equivalent to using the class as a spring configuration xml file |
@Bean | Create bean an object and add it to the container, acting on the method |
Example:
SSM
During the integration of the native framework, if we want to IOC
add a custom object to the container, what we need to do is to add the content declaration of the following tag
under Spring
the 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 SpringBoot
the framework, we are allowed to directly IOC
inject custom objects into the container by creating and returning Java objects.
This process depends on @Configuration
annotation tags and @Bean
annotation tags.
First we prepare a User
class:
@Data //记得要导入Lombok相关的依赖
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String username;
private Integer age;
}
Then create a UserConfiguration
class and use @Configuration
the 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 User
type, and the method name is IOC
the object name of the injected object in the container, and use @Bean
the 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 SpringBoot
the framework to scan and IOC
inject objects into the container is:
- When the SpringBoot framework scans
@Configuration
the type decorated with annotation tags, it will consider this class as a configuration class; @Bean
The return value object of the method decorated with annotation tags in the configuration class will be automatically injectedIOC
into the container for storage;@Bean
The data type of the return value of the annotation tag decoration method is equivalent to the attribute value of the tagclass
;- By default,
@Bean
the method name of the method decorated with the annotation tag is equivalent toid
the attribute value of the tag, that is, theIOC
object name of the object in the container.
In this way, we can XML
easily IOC
inject custom Java objects into the container without using configuration files.
3. @AutoWired
@Autowired
By default, it is assembled by type, which is often used in business layer implementation classes and persistence layer implementation classes
Example:
In TestController
the class, @Autowired
an object is automatically injected through the annotation tag User
, and when test
the method is accessed, the User
object 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 test
the method:
四、@Service、@Controller、@Repository、@Component
annotation | illustrate |
---|---|
@Service | For the business layer, the business logic layer service is injected into spring the container |
@Controller | control layer controller injection spring container |
@Repository | Persistence layer dao injection spring container |
@Component | common domain injection spring container |
五、@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 @Controller and , acting on the class, the effect is equal to adding and @ResponseBody on the class@ResponseBody @Controller |
六、@RequestMapping、@GetMapping、@PostMapping
annotation | illustrate |
---|---|
@RequestMapping | url Use this annotation to access through configuration , which can get be or post |
@GetMapping | Use this annotation to url access through configuration and limit get the request method |
@PostMapping | Use this annotation to url access through configuration and limit post the request method |
七、@RequestParam、@RequestBody
annotation | illustrate |
---|---|
@RequestParam | It is mainly used to receive url? the following parameters, get or post requests, 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 ), get there is no request body, so it is generally used for post requests |
- In
GET
requests, you cannot use@RequestBody
- In the request, and
POST
can be used , but if used , the configuration of parameter conversion must be unified.@RequestBody
@RequestParam
@RequestBody
- Can use multiple
@RequestParam
fetch data,@RequestBody
not