Spring YAML Configuration

One of the ways of configuring Spring applications is using YAML configuration files.

Spring YAML File

1、Spring profiles help enable Spring Applications to define different properties for different environments.
2、application.yml relative path is /myApplication/src/main/resources/application.yml

Let’s take a look at a simple YAML file that contains two profiles. separating by three dashes.

# application.yml
spring:
    config:
        activate:
            on-profile: test
name: test-YAML
environment: testing
enabled: false
servers: 
    - www.abc.test.com
    - www.xyz.test.com

---
spring:
    config:
        activate:
            on-profile: prod
name: prod-YAML
environment: production
enabled: true
servers: 
    - www.abc.com
    - www.xyz.com

The properties defined in the profile-specific documents won’t be loaded unless we explicitly indicate it.

Binding YAML to a Config Class

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties
public class YAMLConfig {
 
    private String name;
    private String environment;
    private boolean enabled;
    private List<String> servers = new ArrayList<>();

    // standard getters and setters

}

The annotations used here are:
1、@Configurationmarks the class as a source of bean definitions
2、@EnableConfigurationPropertiesenable @ConfigurationProperties annotated beans in the Spring application
3、@ConfigurationPropertiesbinds and validates the external configurations to a configuration class

Accessing the YAML Properties

In the properties file, we’ll set the spring.profiles.active environment variable to prod.
If we don’t define this property, only the default profile will be active.

spring.profiles.active=prod
@SpringBootApplication
public class MyApplication implements CommandLineRunner {

    @Autowired
    private YAMLConfig myConfig;

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MyApplication.class);
        app.run();
    }

    public void run(String... args) throws Exception {
        System.out.println("using environment: " + myConfig.getEnvironment());
        System.out.println("name: " + myConfig.getName());
        System.out.println("enabled:" + myConfig.isEnabled());
        System.out.println("servers: " + myConfig.getServers());
    }
}

using environment: production
name: prod-YAML
enabled: true
servers: [www.abc.com, www.xyz.com]

YAML Property Overriding

In Spring Boot, YAML files can be overridden by other YAML properties files.

in order of highest precedence first:
1、Profiles’ properties placed outside the packaged jar
2、Profiles’ properties packaged inside the packaged jar
3、Application properties placed outside the packaged jar
4、Application properties packaged inside the packaged jar

参考:Spring YAML Configuration

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值