系列文章目录
SpringBoot入門學習(一)创建与发布
SpringBoot入门学习笔记(二)配置文件介绍 及 自动配置原理简介
SpringBoot入门学习笔记(三)日志
文章目录
配置文件
SpringBoot使用一个全局的配置文件,配置文件名固定为 application.properties
或 application.yml
。用于修改SpringBoot配置的默认值。
关于区别:
- yml为树型结构,properties需要写全,并以点分割
- 正常的优先加载yml文件,若同时存在yml与properties则最终以properties文件为准。
yml语法
- K : V 表示键值对(注意冒号前后有空格)
- 以空格的缩进来控制层级关系,左对齐的数据,为同一层级
- 属性与值均大小写敏感
- 值的写法:字符、对象(属性与值)(键值对)、数组(List、Set)
示例:
# 对象
person:
name: huathy
age: 18
boss: true
##行内写法
person: {name: huathy,age: 18}
# 数组
users:
- Huathy
- LiSi
# 行内写法
users: [Huathy,LiSi]
properties语法
person.name=huathy
person.age=18
person.boss=false
yml文件取值
- 导入相关依赖
<!-- 导入配置文件处理器,配置文件进行绑定就会有提示 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
- 创建实体
package com.hx.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 将配置文件中配置的每个属性的值映射到该组件中
* @ConfigurationProperties 告诉springBoot将本类中的所有属性和配置文件中相关的配置进行绑定
* (prefix = "person") 配置文件中那个下面的所有属性进行对应的映射
* @Component 只有该组件在容器中才能使用容器提供的功能
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
public String name;
public Integer age;
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
- 创建测试类
/**
* springBoot的单元测试
* @RunWith 指定使用SpringBoot驱动器来测试,而不是junit来测试
* 可以在测试期间很方便的类似编码一样的进行自动注入
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class Sb2YmlApplicationTests {
@Autowired
Person person;
@Test
public void test() {
System.out.println(person);
}
}
注意,@RunWith报错,则springBoot项目创建时自动导入的测试依赖需要将scope(或改为compile编译范围),以及下边的那一段注释掉,如下 示例。如果没有则导入依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- <scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
-->
</dependency>
- 输出示例,及项目结构
- 注意在使用properties文件编写配置文件时,需要注意其编码格式是否支持中文。建议使用utf-8格式编码以支持中文。否则可能乱码。
@Value赋值
@Component
//@ConfigurationProperties(prefix = "person")
public class Person {
@Value("${person-name}")
public String name;
@Value("#{11*2}")
public Integer age;
@Value("true")
public boolean isBoss;
//省略getter、setter、toString
}
@PropertySource & @ImportResource
@PropertySource加载指定配置文件。
示例如下:
- person.properties
person.name=李四
person.age=18
person.boss=false
- Person.java
@PropertySource(value = {"classpath:person.properties"})
//告诉springBoot加载类路径下的person.properties文件,并注入到person对象中
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
public String name;
public Integer age;
public boolean boss;
//此处省略getter、setter、toString方法
}
- 测试方法
@RunWith(SpringRunner.class)
@SpringBootTest
public class Sb2YmlApplicationTests {
@Autowired
Person person;
@Test
public void test() {
System.out.println(person);
}
}
@ImportResource导入Spring的配置文件,使得Spring配置文件生效
示例如下:
- 创建一个HelloService
public class HelloService {
}
- 编写beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="com.hx.service.HelloService"></bean>
</beans>
- 如果这时候测试是没有加载的,我们必须在springBoot主程序上添加@ImportResource注解
@ImportResource(locations = {"classpath:beans.xml"})
//导入Spring的配置文件,使得Spring配置文件生效
@SpringBootApplication
public class Sb2YmlApplication {
public static void main(String[] args) {
SpringApplication.run(Sb2YmlApplication.class, args);
}
}
- 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class Sb2YmlApplicationTests {
@Autowired
ApplicationContext ioc;
@Test
public void testHello() {
System.out.println(ioc.containsBean("hello"));;
}
}
@Bean
这是SpringBoot推荐的使用全注解的方式给spring容器添加组件。
示例如下
- 创建MyAppConfig.java
/**
* @Configuration 告诉springboot这是一个配置类,相当于之前的Spring配置文件
* 在配置文件中使用<bean></bean>标签来添加组件
*/
@Configuration
public class MyAppConfig {
@Bean
//将方法的返回值添加到容器中,容器中这个租价默认的ID就是这个方法的方法名
public HelloService helloService() {
System.out.println("HelloService添加到容器中...");
return new HelloService();
}
}
- 测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class Sb2YmlApplicationTests {
@Autowired
ApplicationContext ioc;
@Test
public void testHello() {
System.out.println("helloService-" + ioc.containsBean("helloService"));;
}
}
好了以上就是说的是关于配置文件相关的。
一般的开发中我们可以采用@Service注解的方式来将service注入到Spring容器中。如下所示:
@Service
public class HelloService {
}
配置文件占位符
- 随机数
${random.int}
${random.int(10)}
${random.long}
${random.int[1024,6666]}
- 占位符获取之前的值,如果没有可以使用冒号来指定默认的值
示例如下:
person.name=李四
person.age=${random.int} #随机数
person.boss=false
person.descript = ${person.name}_${random.uuid}_${person.hello:hi}
#${random.uuid} 生成uuid
#${person.hello:hi}没有person.hello这个值,冒号之后的表示如果没有指定,则默认为hi
Profile多环境支持
多Profile文件,在主配置文件编写时,文件名可以是 application-profile.properties/yml
示例如下:
开发环境配置文件:appliication-dev.properties
生产环境配置文件:application-prod.properties
默认使用application.properties
激活指定的profile方法
在application.properties中写明:spring.profiles.active=dev
yml多文档块注释
yml文件指定
spring:
profiles:
active: prod
---
spring:
profiles: dev
#端口配置
server:
port: 8081
---
spring:
profiles: prod
#端口配置
server:
port: 8082
编译器参数方式指定
- 指定程序运行参数
- 指定虚拟机参数
命令行参数指定
- 指定程序运行参数
java -jar SB_Hello-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
- 指定虚拟机参数
java -jar SB_Hello-0.0.1-SNAPSHOT.jar -Dspring.profiles.active=dev
配置文件加载位置
springboot会按照优先级依次扫描以下位置的配置文件。如果存在相同内容以高优先级为准。文件都会加载,进行互补配置。
- file:./config/ 当前项目文件下的config文件夹中
- file:./ 当前项目的文件目录中
- classpath:/config/ 类路径下的config目录中
- classpath:/ 类路径中
- 在项目打包好后,可以用spring.config.location命令行参数,在启动项目时来指定配置文件新位置,指定的配置文件与以上位置的配置文件同时生效,并进行互补配置。示例:
java -jar xxx.jar --spring.config.location=G:/xxx.properties
外部配置文件加载顺序
SpringBoot亦可从以下位置按照优先级加载配置文件,同时进行互补配置。
配置文件中可以配置的属性
参考官方文档:https://docs.spring.io/spring-boot/docs/2.3.x/reference/htmlsingle/#common-application-properties
SpringBoot自动配置原理
- spirngBoot在启动时加载主配置类,开启自动配置功能
@EnableAutoConfiguration
@EnableAutoConfiguration
注解的作用:利用@Import(AutoConfigurationImportSelector.class)
给容器中导入一些组件
在AutoConfigurationImportSelector.java
中存在以下方法
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata);
return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
}
随后通过SpringFactoriesLoader.loadFactoryNames()
方法扫描jar包类路径下 META-INF/spring.factories,将扫描到的内容包装成properties对象。从properties中获取到EnableAutoConfiguration.class类对应的值,然后把他们添加到容器中。将类路径下 META-INF/spring.factories 中配置的所有 EnableAutoConfiguration 的值加入到容器中。每一个xxxAutoConfiguration类都是容器的一个组件,都加入到容器中,用他们来做配置。
每一个配置类进行自动配置功能。
待完善……