Spring Boot弱化配置的特性让属性配置文件的使用也更加便捷,它默认支持对application.properties或application.yml属性配置文件处理,即在application.properties或application.yml文件中添加属性配置,可以使用@Value注解将属性值注入到beans中,或使用@ConfigurationProperties注解将属性值绑定到结构化的beans中
依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.0.RELEASEversion>
<relativePath/>
parent>
<groupId>org.apdoergroupId>
<artifactId>configartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>configname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.10version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
创建一个 application.yml
,这个创建项目的时候会默认给一个application.properties
,个人习惯改成yml
格式,两种都可以用
首先是application.yml
### 这个配置文件里面是通用配置
server:
port: 9999
spring:
profiles:
active: dev
然后添加不同环境的配置文件dev
,test
和pro
application-dev.yml
spring:
application:
name: pro-config-dev
myproject:
upload:
maxSize: 10
allowTypes:
- image/jpeg-dev
- image/jpg-dev
- image/png-dev
- image/gif-dev
- image/bmp-dev
test: testParmfor@Value-dev
application-test.yml
server:
port: 9999
myproject:
upload:
maxSize: 10
allowTypes:
- image/jpeg-test
- image/jpg-test
- image/png-test
- image/gif-test
- image/bmp-test
test: testParmfor@Value-test
spring:
application:
name: pro-config-test
application-pro.yml
server:
port: 9999
myproject:
upload:
maxSize: 10
allowTypes:
- image/jpeg-pro
- image/jpg-pro
- image/png-pro
- image/gif-pro
- image/bmp-pro
test: testParmfor@Value-pro
spring:
application:
name: pro-config-pro
写一个测试类,这里包含要读取的配置
/**
* @author apdoer
* @version 1.0
* @date 2019/10/22 19:07
*/
@Data
@ToString
@ConfigurationProperties(prefix = "myproject.upload")
public class UploadConfiguration {
private int maxSize;
/**
* 允许的格式
*/
private List<String> allowTypes;
}
controller
里注入使用,这里提供@value
注解引入和spring
的依赖注入都可以
/**
* @author apdoer
* @version 1.0
* @date 2019/10/22 19:05
*/
@RestController
@RequestMapping("/demo")
public class DemoController {
private UploadConfiguration uploadConfiguration;
@Value("${myproject.test}")
private String testParam;
@Autowired(required = false)
public void setUploadConfiguration(UploadConfiguration uploadConfiguration) {
this.uploadConfiguration = uploadConfiguration;
}
@GetMapping("/anno")
public String testParmaByAnnotation(){
return testParam;
}
@GetMapping("/config")
public String testParamByConfig(){
return uploadConfiguration.toString();
}
}
访问localhost:9999/demo/anno
访问 localhost:9999/demo/config
可以看到指定的application-dev.yml
配置文件被读取成功
我们在下图的四个地方分别创建application.yml
分别改变这四个配置文件的指定环境,
比如第一个我把1
里面的配置改成如下
server:
port: 9999
spring:
profiles:
active: pro
访问可以看到这个指定是优先级最高的
可以依次改变各个配置文件的内容,来测试优先级,这里就直接给出来
1 > 4 > 2 >3
就是说如果优先级更高的指定了配置,优先级低的配置就不会生效
欢迎大家加入qq群 859759121
,大量免费vip学习资源,一起成长,一起进步