@Configuration:
- SpringBoot这是一个配置类 == 配置文件,其实也是一个组件;
- @Bean:配置类里面使用@Bean标注在方法上给容器注册组件,默认也是单实例的
- 有两种使用模式
- full模式(有依赖关系,方法会被调用得到之前单实例组件)即proxyBeanMethods=True;
- lite模式(配置 类组件之间无依赖关系用Lite模式加速容器启动过程,减少判断)。
@Configuration(proxyBeanMethods = true)
//proxyBeanMethods=True,是不是代理bean的方法,保持主键单实例
public class Myconfig{
@Bean()
@Bean("设置别名")
public User user(){...}
}
往容器中添加组件:
@Bean:
@Component:
@Controller:
@Service:
@Repository:
@ComponentScan(“com.example”):扫描com.example 包下的组件;
@Import:需要写在容器中其他注解的上边;给容器中自动创建了user、...等组件;默认组件的名字为全类名
@Import{User.class, ....}
@Configuration()
//或......
//@Import{User.class, ...., 其他的class}
//@Component()
—————————————————————
@Conditional....: 条件装配,符合条件组件才进行组件注入。
@ImportRescoure(“classpath:beans.xml”) :允许以spring配置文件的方式对组件进行注册。位置在类的上方。
@Autowired: 自动装配
—————————————————————
配置文件中绑定信息
@ConfigurationPropertier(prefix=”....”) :从配置文件中绑定信息,非常方便。
但封装类需要定义为Spring管理的组件,否则无法进行属性注入.。
//2.第二种方法:在配置类中开启组件自动配置
@EnableConfigurationProperties(Car.class)
//开启car类配置绑定功能 + 将car这个组件自动注册到容器中
//1.第一种方法
@ConfigurationProperties(prefix="Car")
//从配置文件中获取前缀为car的相关信息
public class Car{
private String name;
private Integer price;
}
//2.第二种方法:在配置类中开启组件自动配置
@EnableConfigurationProperties(Car.class)
//开启car类配置绑定功能 + 将car这个组件自动注册到容器中
public class Myconfig{...}
//假如这是配置文件 application.properties
Car.name = BYD
Car.price = 100
@EnableConfigurationProperties(Car.class):开启car的配置绑定功能,开启后Car类中就可以不用写@Component把Car这个组件注册到容器中
————————————————————————
自动配置原理
引导加载自动配置类:
1、@SpringBootApplication:
@SpringBootConfiguration: 相当于声明这是一个配置类@Configuration
@ComponentScan:容器扫描
@Enable Auto Configuration
2、@Enable Auto Configuration
@AutoConfigurationPackage:自动配置包?指定了默认的包规则
@Import(AutoConfigurationImportSelector.class)
@PathVariable:获取路径变量
@RequestHeader:获取请求头
@RequestParam:获取请求参数
@ModelAttribute:、@MatrixVariable、@CookieValue、@RequestBody
@RequestBody:主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);而最常用的使用请求体传参的无疑是POST请求了,所以使用@RequestBody接收数据时,一般都用POST方式进行提交。
在后端的同一个接收方法里,@RequestBody与@RequestParam()可以同时使用,@RequestBody最多只能有一个,而@RequestParam()可以有多个。
@PathVariable: 接受路径变量参数
整合mybaits-plus: