Spring Boot读取yaml的几种方式

在 Spring Boot 中,读取 YAML 配置文件(application.yml )的方式有多种。以下是几种常见的方式以及相关的案例说明:

1. 使用 @Value 注解直接读取 YAML 配置

@Value 注解是 Spring 提供的一种简便方式,可以直接将 YAML 配置中的值注入到字段中。

配置示例 (application.yml):
myapp:
  name: "Spring Boot Application"
  version: "1.0.0"
  features:
    feature1: true
    feature2: false
Java 类使用 @Value 读取配置:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AppConfig {

    @Value("${myapp.name}")
    private String appName;

    @Value("${myapp.version}")
    private String version;

    @Value("${myapp.features.feature1}")
    private boolean feature1;

    public void printConfig() {
        System.out.println("App Name: " + appName);
        System.out.println("Version: " + version);
        System.out.println("Feature 1 Enabled: " + feature1);
    }
}

2. 使用 @ConfigurationProperties 注解将整个配置类映射到 Java 对象

@ConfigurationProperties 注解是 Spring Boot 推荐的方式,它能将 YML 配置文件中的数据绑定到一个 Java 类上,适用于读取复杂的嵌套配置。

配置示例 (application.yml):
myapp:
  name: "Spring Boot Application"
  version: "1.0.0"
  features:
    feature1: true
    feature2: false
Java 类使用 @ConfigurationProperties
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "myapp")
public class MyAppConfig {

    private String name;
    private String version;
    private Features features;

    public static class Features {
        private boolean feature1;
        private boolean feature2;

        // Getters and setters
        public boolean isFeature1() {
            return feature1;
        }

        public void setFeature1(boolean feature1) {
            this.feature1 = feature1;
        }

        public boolean isFeature2() {
            return feature2;
        }

        public void setFeature2(boolean feature2) {
            this.feature2 = feature2;
        }
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public Features getFeatures() {
        return features;
    }

    public void setFeatures(Features features) {
        this.features = features;
    }

    public void printConfig() {
        System.out.println("App Name: " + name);
        System.out.println("Version: " + version);
        System.out.println("Feature 1 Enabled: " + features.isFeature1());
        System.out.println("Feature 2 Enabled: " + features.isFeature2());
    }
}
启用 @ConfigurationProperties

application.yml 中启用 @ConfigurationProperties 的自动绑定(Spring Boot 2.2及以上版本需要加上 @EnableConfigurationProperties 注解):

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableConfigurationProperties(MyAppConfig.class)
public class MyApplication {

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

3. 使用 Environment 对象获取配置

Environment 接口提供了读取配置值的能力,适合在没有注解的情况下,或者在更动态的环境中使用。

配置示例 (application.yml):
myapp:
  name: "Spring Boot Application"
  version: "1.0.0"
  features:
    feature1: true
    feature2: false
使用 Environment 获取配置:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class EnvironmentConfig {

    @Autowired
    private Environment environment;

    public void printConfig() {
        String appName = environment.getProperty("myapp.name");
        String version = environment.getProperty("myapp.version");
        boolean feature1 = Boolean.parseBoolean(environment.getProperty("myapp.features.feature1"));

        System.out.println("App Name: " + appName);
        System.out.println("Version: " + version);
        System.out.println("Feature 1 Enabled: " + feature1);
    }
}

4. 使用 @PropertySource 加载 YAML 配置文件

Spring Boot 默认会加载 application.ymlapplication.properties 文件,但如果你需要加载其他外部的 YAML 配置文件,可以使用 @PropertySource 来指定。

配置示例 (my-config.yml`):
my:
  setting: "Some other config"
Java 类使用 @PropertySource
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:my-config.yml")
public class myConfig {

    @Value("${my.setting}")
    private String externalSetting;

    public void printExternalSetting() {
        System.out.println("other Setting: " + externalSetting);
    }
}

总结:

  • @Value:适合简单的配置项注入。
  • @ConfigurationProperties:适用于复杂的多层嵌套配置,推荐使用。
  • Environment:用于动态获取配置,灵活但较为冗长。
  • @PropertySource:用于加载额外的配置文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昔我往昔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值