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/#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

 

Spring Boot 中,可以使用 `@PropertySource` 注解来引入外部的属性文件,并使用 `@Value` 注解来读取属性值。 如果你想在父项目中读取子项目的配置文件,可以在子项目的 `application.properties` 或 `application.yml` 文件中定义需要读取的属性,然后在父项目中通过 `@PropertySource` 注解来引入子项目的配置文件。 举个例子,如果你想在父项目中读取子项目中的 `application.properties` 文件中的属性,可以在父项目的配置类中加上以下注解: ```java @Configuration @PropertySource("classpath:/application.properties") public class AppConfig { // ... } ``` 其中,`classpath:` 表示在 classpath 中查找文件,因此这个示例代码会在子项目的 classpath 中查找 `application.properties` 文件。 接下来,你就可以使用 `@Value` 注解来读取子项目中的属性了,例如: ```java @Component public class MyComponent { @Value("${my.property}") private String myProperty; // ... } ``` 其中,`${my.property}` 就是子项目中的属性名。 如果子项目使用的是 YAML 文件,那么 `@PropertySource` 注解需要写成这样: ```java @Configuration @PropertySource("classpath:/application.yml", factory = YamlPropertySourceFactory.class) public class AppConfig { // ... } ``` 需要注意的是,这里需要指定 `factory` 属性为 `YamlPropertySourceFactory.class`,表示使用 YAML 格式的属性文件。同时,在读取属性时也需要使用 `@Value` 注解,并使用 YAML 的语法来读取,例如: ```java @Component public class MyComponent { @Value("${my.property}") private String myProperty; // ... } ``` 其中,`${my.property}` 就是 YAML 文件中的属性名。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值