SpringBoot原理
1.配置优先级
SpringBoot中,又三类配置文件,分别为
- application.properties
- application.yml
- application.yaml
在SpringBoot项目当中,要想配置一个属性,可以通过这三种方式当中的任意一种来配置都可以,如果项目中同时存在这三种配置文件,且都配置了同一个属性,那么就有一个重点,系统会优先运行哪一份文件.
假设属性:端口号
- application.properties
server.port=8081
- application.yml
server: port: 8082
- application.yaml
server: port: 8082
启动SpringBoot程序,测试结果为:
properties>yml>yaml
在SpringBoot项目当中除了以上3种配置文件外,SpringBoot为了增强程序的扩展性,除了支持配置文件的配置方式以外,还支持另外两种常见的配置方式:
- Java系统属性配置 (格式: -Dkey=value)
-Dserver.port=9000- 命令行参数 (格式:--key=value)
--server.port=10010
在SpringBoot项目当中,常见的属性配置方式有5种, 3种配置文件,加上2种外部属性的配置(Java系统属性、命令行参数),其优先级为
命令行参数>java系统属性>properties>yml>yaml
2.Bean的管理
2.1bean的作用域
在IOC容器中,默认bean对象是单例的,在spring中支持五种作用域,后三种在web环境下才生效
| 作用域 | 说明 |
|---|---|
| singleton | 容器内同名称的bean只有一个实例(单例)(默认) |
| prototype | 每次使用该bean时会创建新的实例(非单例) |
| request | 每个请求范围内会创建新的实例(web环境中,了解) |
| session | 每个会话范围内会创建新的实例(web环境中,了解) |
| application | 每个应用范围内会创建新的实例(web环境中,了解) |
2.2作用域设置
2.2.1第一种方法
控制器
//默认bean的作用域为:singleton (单例)
@RestController
@RequestMapping("/depts")
public class DeptController {
@Autowired
private DeptService deptService;
public DeptController(){
System.out.println("DeptController constructor ....");
}
//省略其他代码...
}
测试类
@SpringBootTest
class SpringbootWebConfig2ApplicationTests {
@Autowired
private ApplicationContext applicationContext; //IOC容器对象
//bean的作用域
@Test
public void testScope(){
for (int i = 0; i < 10; i++) {
DeptController deptController = applicationContext.getBean(DeptController.class);
System.out.println(deptController);
}
}
}
2.2.2第二种方法
修改控制器DeptController代码:
@Scope("prototype") //bean作用域为非单例
@RestController
@RequestMapping("/depts")
public class DeptController {
@Autowired
private DeptService deptService;
public DeptController(){
System.out.println("DeptController constructor ....");
}
//省略其他代码...
}
2.3第三方bean
学习完bean的获取、bean的作用域之后,接下来我们再来学习第三方bean的配置。
之前我们所配置的bean,像controller、service,dao三层体系下编写的类,这些类都是我们在项目当中自己定义的类(自定义类)。当我们要声明这些bean,也非常简单,我们只需要在类上加上@Component以及它的这三个衍生注解(@Controller、@Service、@Repository),就可以来声明这个bean对象了。
但是在我们项目开发当中,还有一种情况就是这个类它不是我们自己编写的,而是我们引入的第三方依赖当中提供的,那么此时我们是无法使用 @Component 及其衍生注解来声明bean的,此时就需要使用@Bean注解来声明bean 了。
方式一:
在启动类中直接声明这个Bean。
public class TliasWebManagementApplication {
public static void main(String[] args) {
SpringApplication.run(TliasWebManagementApplication.class, args);
}
@Bean
public AliyunOSSOperator aliyunOSSOperator(AliyunOSSProperties ossProperties) {
return new AliyunOSSOperator(ossProperties);
}
}
测试:
@SpringBootTest
class TliasWebManagementApplicationTests {
@Autowired
private AliyunOSSOperator aliyunOSSOperator;
@Test
void testUploadFiles() throws Exception {
// 上传文件, 本地文件 C:\Users\deng\Pictures\6.jpg
byte[] content = FileUtil.readBytes(new File("C:\\Users\\deng\\Pictures\\6.jpg"));
String url = aliyunOSSOperator.upload(content, "6.jpg");
System.out.println(url);
}
@Test
void testListFiles() throws Exception {
// 获取文件列表
List<String> objectNameList = aliyunOSSOperator.listFiles();
objectNameList.forEach(System.out::println);
}
@Test
void testDelFiles() throws Exception {
// 删除文件
aliyunOSSOperator.deleteFile("2024/06/43b48632-3a05-4e1d-a00d-96f2d57b2a84.jpg");
}
}
方式二:
对这些bean进行集中分类配置,可以通过 @Configuration 注解声明一个配置类.
@Configuration
public class OSSConfig {
@Bean
public AliyunOSSOperator aliyunOSSOperator(AliyunOSSProperties ossProperties) {
return new AliyunOSSOperator(ossProperties);
}
}
3. SpringBoot原理
3.1一个注解启动类上 SpringBootApplication 注解,它是一个复合注解,内部有三个注解组成
- SpringBootConfiguration,底层是一个Configuration,表示启动类也是一个配置类,可以在内部定义相关Bean
- ComponentScan,表示组件扫描,默认会扫描启动类包及子包下Spring能识别的组件
- EnableAutoConfiguration,底层是一个Import注解,该注解中导入 AutoConfigurationImportSelector类,该类中有一个 selectImports 方法会被调用,该方法最终会去加载 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中的内容,并筛选满足starter和各种Condtional条件的全类名,封装成一个字符串数组加载到内存中
3.2一个方法
启动类会通过run方法去创建与初始化Spring容器,在初始化时会把上面注解加载到内存中的字符串数组对应的内容,创建成对象注入到Spring容器,至此就完成了自动化装配
浙公网安备 33010602011771号