Spring Boot Configuration 配置檔案讀取以及自定義配置檔案
阿新 • • 發佈:2018-12-07
新增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/#getting-started-first-application-auto-configuration 24. Externalized Configuration
Spring Boot 配置優先順序順序:https://www.cnblogs.com/softidea/p/5759180.html
application.yml和 bootstrap.yml區別:https://www.cnblogs.com/BlogNetSpace/p/8469033.html