前言
记录学习历程,在学习笔记中有描述不正确的地方,欢迎小伙伴们评论指正。
基础
spring-boot有三种配置文件的方式:properties、yaml、yml。spring-boot会自动加载properties、yaml和yml配置文件(yaml和yml是同一种格式,只是后缀名不一样)。当这三种配置文件同时存在时,加载顺序为:properties > yaml > yml 。当三种配置文件中的配置信息有冲突时,properties配置文件会覆盖后两种配置文件中的配置信息。
格式
properties配置文件是以key=value的配置形式。
setver.port=9000
yml配置文件是以key: value的配置形式。
server:
port: 9000
注:yml配置文件冒号后有一个空格,yml配置文件是使用俩个空格缩进。
配置文件
配置文件可以存在与四种位置:
1、项目根目录的/config文件夹下
2、target文件夹下
3、resources/config文件夹下
4、resources文件夹下
若一个同名同类型的配置文件同时存在这四种位置,则它们的覆盖顺序为:1 > 2 > 3 > 4
多环境支持
在spring-boot项目中存在不同环境的配置文件切换,通过创建application-{profile}.properties/.yml文件,{profile}是环境的标识名称(例如:application-dev.properties、application-test.properties)。进行多环境配置文件的切换则使用
spring.profiles.active=dev
命令行添加属性配置
# 示例命令格式
java -jar xxxxx.jar --server.port=9100
若要屏蔽以上这种添加属性方式,则需要添加代码设置
SpringApplication.setAddCommandLineProperties(false);
多配置文件
spring-boot项目配置文件信息较多时,一般会将他们按用途分为多个配置文件(例如:log4j.properties、mq.properties等),spring-boot同样支持对这些文件的加载。除了可以使用spring.config.location环境属性引用一个明确路径外(多个使用逗号分隔),还可以在配置文件中使用spring.peofiles.include属性来实现(多个使用逗号分隔)
spring.profiles.include=log4j,mq
也可以使用spring.config.location命令行参数,key名需要使用下划线代替句号分隔符。
java -jar spring-boot-2.jar --spring.config.location=classpath:/log4j.properties,classpath:/mq.properties
@Value
使用@Value注解,可以把属性配置文件中的值注入到bean中
server.port=8001
server.address=127.0.0.1
testProperties.name=张三
testProperties.age=12
@Value("${testProperties.name}")
private String name;
@Value("${testProperties.age}")
private Integer age;
@Value("${server.address}")
private String ip;
当使用@Value注解获取中文乱码时。首先File>>Settings>>Editor>>File Encodings
依旧乱码后,在配置文件中配置就可以完美解决
server.servlet.encoding.force=true
server.http.encoding.charset=UTF-8
server.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
当属性配置文件变多时,使用@Value注解会相当的麻烦,这时使用@ConfigurationProperties注解来实现属性注入。
@ConfigrationProperties
练习示例:
cp.tcp.name=李四
cp.tcp.age=20
cp.tcp.ip=192.168.2.111
//不用@EnableConfigurationProperties的情况需要使用@Component注解让Component Scan扫描到
@Component
@ConfigurationProperties(prefix = "cp.tcp")
public class TestConfigurationProperties {
private String name;
private Integer age;
private String ip;
}
@RestController
@RequestMapping("/tt")
public class Test1Controller {
@Autowired
private TestConfigurationProperties testConfigurationProperties;
@GetMapping("/showProperties")
public String showProperties(){
return testConfigurationProperties.getName()+";"+testConfigurationProperties.getAge();
}
}
//使用@EnableConfigurationProperties的情况
@ConfigurationProperties(prefix = "cp.tcp")
public class TestConfigurationProperties {
private String name;
private Integer age;
private String ip;
}
@RestController
@RequestMapping("/tt")
@EnableConfigurationProperties(TestConfigurationProperties.class)
public class Test1Controller {
@Autowired
private TestConfigurationProperties testConfigurationProperties;
@GetMapping("/showProperties")
public String showProperties(){
return testConfigurationProperties.getName()+";"+testConfigurationProperties.getAge();
}
}
//或在启动类中加@EnableConfigurationProperties都可以
@SpringBootApplication
@EnableConfigurationProperties(TestConfigurationProperties.class)
public class LearnBasicApplication {
public static void main(String[] args) {
SpringApplication.run(LearnBasicApplication.class, args);
}
}