添加configuration maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
读取核心配置文件application.properties和application.yml
在application.properties下添加如下代码
test.name=test
test.age=22
配置实体类:
第一种:
@Component//使用@Configuration也可以
public class Test {
@Value("${test.name}")
String name;
@Value("${test.age}")
String age;
...
}
测试:localhost:8091/ 成功获取
第二种: 添加前缀读取
@Component
@ConfigurationProperties(prefix = "test")
public class Test {
String name;
String age;
...
}
测试:localhost:8091/ 成功获取
启动and测试类
@SpringBootApplication
@RestController
public class SpringCloudConfigServerApplication {
@Autowired
private Test test;
@RequestMapping("/")
public Map<String, Object> sayHello() {
Map<String, Object> result = new HashMap<String, Object>();
result.put("name", test.getName());
result.put("age", test.getAge());
return result;
}
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}
读取自定义配置文件
创建test.properties,内容如下:
test.name=chen
test.age=22
需要添加@PropertySource注解 获取文件路径 (String [] value();) 可以扫描多个
@Component
@ConfigurationProperties(prefix = "test")
@PropertySource(value = "classpath:test.properties")
public class Test {
String name;
String age;
...
}
测试:localhost:8091
多环境配置文件加载
application-dev.properties:开发环境
application-test.properties:测试环境
application-prod.properties:生产环境
创建多个配置文件:
在application.properties 中通过spring.profiles.active={profile}来设置加载对应的环境配置文件
spring.profiles.active=prod
测试:
基本就是这样了。
Sping Boot Configuration的加载顺序:
- Devtools global settings properties on your home directory (
~/.spring-boot-devtools.properties
when devtools is active). @TestPropertySource
annotations on your tests.@SpringBootTest#properties
annotation attribute on your tests.- Command line arguments.
- Properties from
SPRING_APPLICATION_JSON
(inline JSON embedded in an environment variable or system property). ServletConfig
init parameters.ServletContext
init parameters.- JNDI attributes from
java:comp/env
. - Java System properties (
System.getProperties()
). - OS environment variables.
- A
RandomValuePropertySource
that has properties only inrandom.*
. - Profile-specific application properties outside of your packaged jar (
application-{profile}.properties
and YAML variants). - Profile-specific application properties packaged inside your jar (
application-{profile}.properties
and YAML variants). - Application properties outside of your packaged jar (
application.properties
and YAML variants). - Application properties packaged inside your jar (
application.properties
and YAML variants). @PropertySource
annotations on your@Configuration
classes.- Default properties (specified by setting
SpringApplication.setDefaultProperties
).
参考spring boot官方文档:https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/htmlsingle/#boot-features-external-config
Spring Boot 配置优先级顺序:https://www.cnblogs.com/softidea/p/5759180.html
application.yml和 bootstrap.yml区别:https://www.cnblogs.com/BlogNetSpace/p/8469033.html