配置文件
配置文件
SpringBoot使用一个全局的配置文件,文件名字在约定的情况加是固定的名为:application.properties或者application.yml
配置文件的加载顺序为:
<includes>
<include>**/application*.yml</include>
<include>**/application*.yaml</include>
<include>**/application*.properties</include>
</includes>
官方给的解释为:以先读取到的配置为主,后读取到的作为补充
配置文件的优先顺序可能会随着版本的变更而改变
加载顺序
- 根路径下的config文件夹
- 根路径下
- classpath下的config文件夹
- classpath下

组织多文件 Import
需求:如果继承过多的第三方框架,而第三方框架所有的配置全部都集中在application文件中会导致配置文件的臃肿,所以会把一些框架配置内容分散到其他的配置文件中,在使用Import进行导入使用
格式(多个文件使用逗号作为分隔符):spring.config.import=[classpath|configserver|configtree|file|optional]xxxx/xxxx.[properties|yaml|yml]
多环境配置 Profile
在不同的环境下使用不同的配置;SpringBoot框架提供了多Profile的管理功能
官方语法:application-{profile}.(properties|yaml|yml)
例:在开发环境和生产环境中有两个配置文件

只需要在application.properties配置文件中使用spring.profiles.active=
后面跟上{profile}即可使用不同的配置,如:
spring.profiles.active=prod
绑定Bean
Value
在实体类中,使用@Value("${xx.xx:xx[默认值可不写]}")
作用在属性上,最后在配置文件中进行编写即可注入(不支持松散绑定)
@Value("${user.username}")
private String username;
// Get/Set
user.username=admin
注意:在使用properties时无法使用中文进行注入
ConfigurationProperties
在实体类上加入@ConfigurationProperties(prefix = "user")
注解prefix中,这样就会把user.xxx相对应的内容映射到属性中(支持松散绑定)
/*
* @Component 使用该注解也行主要是为了创建对象
* 下面注解也可以它继承了上面的注解
* */
// 表示当前是一个配置类并禁用代理bean方法
@Configuration(proxyBeanMethods = false)
// 指定数据源 需要配合Configuration一同使用
@PropertySource(value = "classpath:/application.properties")
// 读取配置文件(注解不可单独使用)
@ConfigurationProperties(prefix = "user")
public class User {
// 非静态属性
private String name;
private String age;
private String sex;
}
user.username=admin
user.age=19
扫描注解
@ConfigurationProperties
注解起作用需要在启动类上使用 @EnableConfigurationProperties
或者 @ConfigurationPropertiesScan
,这两个注解是专门用来寻找 @ConfigurationProperties
注解的,将注解的对象注入到Spring容器中
// 如果过于分散使用以下内容(精准定位)
@EnableConfigurationProperties({User.class})
// 如果集中在同一个包中使用以下注解(范围打击)
@ConfigurationPropertiesScan(basePackages = {"top.xmln.newspringboot.pojo"})
第三方
@ConfigurationProperties
注解仅仅针对自己写的类,无法直接对第三方的类进行添加,但是可以与 @Bean
一起在方法上使用
@Configuration // 万万不可写 proxyBeanMethods = false 就是靠代理进行赋值
public class ApplicationConfig {
@Bean // 创建Bean对象,会把配置文件中的内容赋值给User中的属性中(名称要一致)
@ConfigurationProperties(prefix = "user")
public User user() {
return new User();
}
}
@Data // 假装我是一个第三方的类
class User {
private String username;
private String password;
}
基本类型插入(properties)
Java实体类
private String username;
private Integer age;
private Date birthday;
private List<String> hobby;
private Map<String, String> map;
private Car car;
properties配置文件
user.username=admin
user.age=19
# 日期类型
user.birthday=2024/03/17 00:00:01
# List集合
user.hobby[0]=1
user.hobby[1]=2
user.hobby=3,4,5
# Map集合
user.map.key1=value1
user.map.key2=value2
# 插入对象
user.car.name=luhu
user.car.price=123
随机值
# 随机数字字母组合
${random.value}
# UUID
${random.uuid}
# 随机int值
${random.int}
# 100以内的随机int值
${random.int(100)}
# 从1024开始到65535的随机值
${random.int[1024,65535]}
# 随机的long值
${random.long}
# 100以内的随机long值
${random.long(100)}
# 从1024开始到65535的随机值
${random.long[1024,65535]}
数据校验
数据校验在类上添加@Validated
,仅支持@ConfigurationProperties
不支持@Value
注解 | 描述 |
---|---|
Null | 被注释的元素必须为nul |
NotNull | 被注释的元素必须不为null |
AssertTrue | 被注释的元素必须为true |
AssertFalse | 被注释的元素必须为false |
Min(value) | 被注释的元素必须是一个数字,其值必须大于等于指定的最小值 |
Max(value) | 被注释的元素必须是一个数字,其值必须小于等于指定的最大值 |
DecimalMin(value) | 被注释的元素必须是一个数字,其值必须大于等于指定的最小值 |
DecimalMax(value) | 被注释的元素必须是一个数字,其值必须小于等于指定的最大值 |
SIze(max,min) | 被注释的元素的大小必须在指定的范围内 |
Digits(integer,fraction) | 被注释的元素必须是一个数字,其值必须在可接受的范围内 |
Past | 被注释的元素必须是一个过去的日期 |
Future | 被注释的元素必须是一个将来的日期 |
Pattern(value) | 被注释的元素必须符合指定的正则表达式 |
需要添加数据校验的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
外部自定义配置文件(PropertySource)
使用 @PropertySource
注解,它不仅能读取properties文件,也能读取xml文件,并且通过YAML解析器,配合自定义PropertySourceFactory实现解析YAML文件
@PropertySource("classpath:/data/application.properties")
Environment
Environment
是多种数据来源的集合,从中可以读取application配置文件、环境变量、系统属性,相当于外部的属性都汇聚到了 Environment
里面,使用时直接注入使用即可
// 获取环境变量
String property1 = environment.getProperty("JAVA_HOME");
// 获取配置信息
String property2 = environment.getProperty("spring.application.name");
// 获取配置信息,有默认值、指定类型
String property3 = environment.getProperty("spring.application.name", String.class, "admin");
// 判断属性是否存在
boolean b = environment.containsProperty("JAVA_HOME_21");
创建对象的三种方式
- XML方式
- Java Config技术,@Configuration 与 @Bean
- 创建对象的注解,@Controller,@Service,@Repository,@Component
在SpringBoot中不建议使用xml配置文件,如果需要xml提供Bean的声明
@ImportResource(locations = {"xxx.xml"})
加载xml注册Bean