1. 程式人生 > >Spring Boot Configuration 配置檔案讀取以及自定義配置檔案

Spring Boot Configuration 配置檔案讀取以及自定義配置檔案

新增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的載入順序:

  1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
  2. @TestPropertySource annotations on your tests.
  3. @SpringBootTest#properties annotation attribute on your tests.
  4. Command line arguments.
  5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  6. ServletConfig init parameters.
  7. ServletContext init parameters.
  8. JNDI attributes from java:comp/env.
  9. Java System properties (System.getProperties()).
  10. OS environment variables.
  11. RandomValuePropertySource that has properties only in random.*.
  12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
  13. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
  14. Application properties outside of your packaged jar (application.properties and YAML variants).
  15. Application properties packaged inside your jar (application.properties and YAML variants).
  16. @PropertySource annotations on your @Configuration classes.
  17. 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