1. 配置属性注入
Spring Boot 使用全局的配置文件,配置文件名是固定的:application.properties
或者 application.yml
这两个配置文件可以同时存在,如果配置相同的属性,properties 会覆盖 yml
1.1 yml 语法
1.1.1 基本语法
k: v
:表示一对键值对,注意 value 前面必须要有空格
以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的
server:
port: 8081
servlet:
context-path: /demo
1.1.2 值的写法
1. 字面量:普通的值(数字,字符串,布尔)
k: v
:字面直接来写
""
:双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思,如下会输出 zhangsan 换行 lisi
name: "zhangsan \n lisi"
''
:单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据,如下会输出 zhangsan \n lisi
name: 'zhangsan \n lisi'
2. 对象、Map(属性和值)(键值对)
k: v
:在下一行来写对象的属性和值的关系;注意缩进
people:
lastName: zhangsan
age: 20
people: {lastName: zhangsan,age: 18}
3. 数组(List、Set)
用 -
值表示数组中的一个元素
pets:
- cat
- dog
- pig
pets: [cat,dog,pig]
1.2 配置文件值注入
代码已经上传至 https://github.com/masteryourself-tutorial/tutorial-spring ,详见
tutorial-spring-boot-core/tutorial-spring-boot-config
工程
1.2.1 配置文件
1. application.properties
person.last-name=张三
person.age=18
person.boss=false
person.birth=2018/12/12
person.maps.k1=v1
person.maps.k2=12
person.lists=lisi,19,wahaha
person.dog.name=wangwang
person.dog.age=3
1.2.2 核心代码
1. PersonProperties
@ConfigurationProperties
:告诉 Spring Boot 将本类中的所有属性和配置文件中相关的配置进行绑定
prefix = "person"
:配置文件中的前缀
只有这个组件是容器中的组件,才能容器提供的 @ConfigurationProperties
功能,也可以使用 @EnableConfigurationProperties(PersonProperties.class)
导入组件
@Data
@ConfigurationProperties(prefix = "person")
// @Component
public class PersonProperties {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private DogProperties dog;
}
2. DogProperties
@Data
public class DogProperties {
private String name;
private Integer age;
}
3. ConfigApplication
这里使用 @EnableConfigurationProperties(PersonProperties.class)
将 PersonProperties 导入到 Spring 容器中
@SpringBootApplication
@EnableConfigurationProperties(PersonProperties.class)
public class ConfigApplication implements EnvironmentAware, ApplicationContextAware {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
@Override
public void setEnvironment(Environment environment) {
System.out.println("test.name1" + environment.getProperty("test.name1"));
System.out.println("test.name2" + environment.getProperty("test.name2"));
System.out.println("test.name3" + environment.getProperty("test.name3"));
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println(applicationContext.getBean(PersonProperties.class));
}
}
1.3 @Value & @ConfigurationProperties
@ConfigurationProperties | @Value | |
---|---|---|
功能 | 批量注入配置文件中的属性 | 一个个指定 |
松散绑定(松散语法) | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303数据校验 | 支持 | 不支持 |
复杂类型封装 | 支持 | 不支持 |
如果只是在某个业务逻辑中需要获取一下配置文件中的某项值,推荐使用 @Value
如果专门编写了一个 JavaBean 来和配置文件进行映射,推荐使用 @ConfigurationProperties
+ @EnableConfigurationProperties
1.4 @PropertySource
加载指定的配置文件
1.4.1 配置文件
1. teacher.properties
teacher.name=李四老师
teacher.age=18
1.4.2 代码
2. ConfigApplication
@SpringBootApplication
@EnableConfigurationProperties(PersonProperties.class)
@PropertySource(value = {"classpath:teacher.properties"})
@ImportResource(value = {"classpath:spring-bean.xml"})
public class ConfigApplication implements EnvironmentAware, ApplicationContextAware {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
@Override
public void setEnvironment(Environment environment) {
// test.name1:zhangsan \n lisi
System.out.println("test.name1:" + environment.getProperty("test.name1"));
// test.name2:zhangsan
// lisi
System.out.println("test.name2:" + environment.getProperty("test.name2"));
// test.name3:zhangsan \n lisi
System.out.println("test.name3:" + environment.getProperty("test.name3"));
// teacher.name:李四老师
System.out.println("teacher.name:" + environment.getProperty("teacher.name"));
// teacher.age:18
System.out.println("teacher.age:" + environment.getProperty("teacher.age"));
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// PersonProperties:PersonProperties(lastName=张三, age=18, boss=false, birth=Wed Dec 12 00:00:00 CST 2018, maps={k1=v1, k2=12}, lists=[lisi, 19, wahaha], dog=DogProperties(name=wangwang, age=3))
System.out.println("PersonProperties:" + applicationContext.getBean(PersonProperties.class));
// SpringBeanService:SpringBeanService bean 实例创建成功了
System.out.println("SpringBeanService:" + applicationContext.getBean(SpringBeanService.class));
}
}
1.5 @ImportResource
导入 Spring 的 xml 配置文件
1.5.1 配置文件
1. spring-bean.xml
<bean id="springBeanService" class="pers.masteryourself.study.spring.boot.config.service.SpringBeanService"/>
1.5.2 代码
2. ConfigApplication
@SpringBootApplication
@EnableConfigurationProperties(PersonProperties.class)
@PropertySource(value = {"classpath:teacher.properties"})
@ImportResource(value = {"classpath:spring-bean.xml"})
public class ConfigApplication implements EnvironmentAware, ApplicationContextAware {
1.6 @Conditional 派生注解
必须是 @Conditional
指定的条件成立,才给容器中添加组件,配置类里面的所有内容才生效
@Conditional扩展注解 | 作用(判断是否满足当前指定条件) |
---|---|
@ConditionalOnJava | 系统的 Java 版本是否符合要求 |
@ConditionalOnBean | 容器中存在指定 Bean |
@ConditionalOnMissingBean | 容器中不存在指定 Bean |
@ConditionalOnExpression | 满足 SpEL 表达式指定 |
@ConditionalOnClass | 系统中有指定的类 |
@ConditionalOnMissingClass | 系统中没有指定的类 |
@ConditionalOnSingleCandidate | 容器中只有一个指定的 Bean,或者这个 Bean 是首选 Bean |
@ConditionalOnProperty | 系统中指定的属性是否有指定的值 |
@ConditionalOnResource | 类路径下是否存在指定资源文件 |
@ConditionalOnWebApplication | 当前是 web 环境 |
@ConditionalOnNotWebApplication | 当前不是 web 环境 |
@ConditionalOnJndi | JNDI 存在指定项 |
2. profile
2.1 多 profile 环境
在编写配置文件时,文件名可以是 application-{profile}.properties/yml
默认使用 application.properties
的配置
2.2 多 profile 示例
1. application.properties
server.port=8080
## 激活 uat 环境, 如果不激活默认使用 8080
# spring.profiles.active=uat
2. application.yml
yml 支持多文档块方式,即以 ---
来区分
## 指定属于 uat 环境
spring:
profiles: uat
server:
port: 8083
---
## 指定属于 prod 环境
spring:
profiles: prod
server:
port: 8084
3. application-dev.properties
server.port=8081
4. application-test.yml
server:
port: 8081
2.3 激活 profile
代码已经上传至 https://github.com/masteryourself-tutorial/tutorial-spring ,详见
tutorial-spring-boot-core/tutorial-spring-boot-profile
工程
2.3.1 配置文件
在配置文件中指定 spring.profiles.active=uat
激活 uat 环境
2.3.2 命令行参数
如果是打成 jar 包,则加上 --spring.profiles.active=uat
java -jar study-spring-boot-profile-1.0.0-SNAPSHOT.jar --spring.profiles.active=uat
如果是在 IDEA 中,则在 Program arguments
中添加配置
2.3.3 虚拟机参数
如果是打成 jar 包,则加上 -Dspring.profiles.active=uat
, 注意和命令行参数的位置不同
java -jar -Dspring.profiles.active=uat study-spring-boot-profile-1.0.0-SNAPSHOT.jar
如果是在 IDEA 中,则在 VM options
中添加配置
2.3.4 单元测试
可以使用 @ActiveProfiles
来激活某个环境
@RunWith(SpringRunner.class)
@ActiveProfiles({"dev", "test"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class ProfileApplicationTest {
3. 加载顺序
3.1 配置文件加载顺序
Spring Boot 启动会扫描以下位置的 application.properties
或者 application.yml
文件作为 Spring Boot 的默认配置文件,优先级由高到底,高优先级的配置会覆盖低优先级的配置
可以通过 --spring.config.location=D:/application.properties
来改变默认的配置文件位置
file:./config/ :即当前路径下的 config 目录
file:./ :即当前项目路径
classpath:/config/ :即类路径文件下的 config 目录
classpath:/ :即类路径的根目录,这也是 application.properties 文件的默认位置
3.2 外部配置加载顺序
Spring Boot 也可以从以下位置加载配置,优先级从高到低,高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置
-
命令行参数
-
来自
java:comp/env
的 JNDI 属性 -
Java系统属性(
System.getProperties()
) -
操作系统环境变量
-
RandomValuePropertySource 配置的
random.*
属性值 -
jar 包外部的
application-{profile}.properties
或application.yml
(带 spring.profile) 配置文件 -
jar 包内部的
application-{profile}.properties
或application.yml
(带 spring.profile) 配置文件 -
jar 包外部的
application.properties
或application.yml
(不带 spring.profile) 配置文件 -
jar 包内部的
application.properties
或application.yml
(不带 spring.profile) 配置文件 -
@Configuration
注解类上的@PropertySource
-
通过
SpringApplication.setDefaultProperties()
指定的默认属性