在Spring Boot中获取YML配置字段值的方法

在Spring Boot中,有几种常见的方式可以获取application.yml或application.properties文件中的配置值:

图片[1]_在Spring Boot中获取YML配置字段值的方法_知途无界

1. 使用@Value注解

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {

    @Value("${my.config.field}")
    private String configField;

    @Value("${my.config.number:100}") // 带默认值
    private int configNumber;

    public void printConfig() {
        System.out.println("Config field: " + configField);
        System.out.println("Config number: " + configNumber);
    }
}

对应的YML配置:

my:
  config:
    field: "Hello World"
    number: 42

2. 使用@ConfigurationProperties注解

对于复杂或分组的配置,推荐使用这种方式:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "my.config")
public class MyConfigProperties {
    private String field;
    private int number;
    private List<String> list;

    // getters and setters
    public String getField() { return field; }
    public void setField(String field) { this.field = field; }

    public int getNumber() { return number; }
    public void setNumber(int number) { this.number = number; }

    public List<String> getList() { return list; }
    public void setList(List<String> list) { this.list = list; }
}

对应的YML配置:

my:
  config:
    field: "Hello World"
    number: 42
    list:
      - item1
      - item2
      - item3

3. 使用Environment接口

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {
    private final Environment env;

    public MyComponent(Environment env) {
        this.env = env;
    }

    public void printConfig() {
        String field = env.getProperty("my.config.field");
        int number = env.getProperty("my.config.number", Integer.class, 100);

        System.out.println("Config field: " + field);
        System.out.println("Config number: " + number);
    }
}

4. 嵌套配置类

对于更复杂的嵌套配置:

@ConfigurationProperties(prefix = "my.config")
public class MyConfigProperties {
    private Database database;
    private Security security;

    // getters and setters
    public static class Database {
        private String url;
        private String username;
        private String password;
        // getters and setters
    }

    public static class Security {
        private boolean enabled;
        private String secret;
        // getters and setters
    }
}

对应的YML配置:

my:
  config:
    database:
      url: jdbc:mysql://localhost:3306/mydb
      username: root
      password: secret
    security:
      enabled: true
      secret: abc123

注意事项

  1. 使用@ConfigurationProperties需要在主类或配置类上添加@EnableConfigurationProperties注解,或直接在配置类上使用@Component
  2. 对于@ConfigurationProperties类,必须提供setter方法或使用构造函数绑定(Spring Boot 2.2+)
  3. 可以在IDEA中安装Spring Boot插件,它会为YML文件提供自动补全和验证功能
  4. 对于生产环境,敏感信息建议使用环境变量或配置中心,而不是直接写在YML文件中
© 版权声明
THE END
喜欢就点个赞,支持一下吧!
点赞87 分享
评论 抢沙发
头像
欢迎您留下评论!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容