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

猜你喜欢

转载自blog.csdn.net/qq_38423105/article/details/81902806