SpringBoot 获取配置文件属性(全5种,附项目Demo)

时间:2025-03-19 07:19:41

一. 使用 @PropertySource + Environment

@PropertySource 指定配置文件位置, Environment 读取配置文件属性。

@Configuration
@PropertySource(value = {"classpath:"})
public class TestConfig {

    @Autowired
    private Environment env;

    public Map<String, Object> map=new HashMap<>();

    @Bean
    public Map<String, Object> test() {
        String testName = ("");
        Integer testId = ((""));
        ("test1 -> | testName -> " + testName + " | test id -> " + testId);
        ("name", testName);
        ("id", testId);
        return map;
    }

}

二. 使用 @PropertySource + @Value

@PropertySource 指定配置文件位置,@Value 指定 `key对应的值。

@Configuration
@PropertySource(value = {"classpath:"})
public class Test2Config {

    @Value("${}")
    private String testName;

    @Value("${}")
    private Integer testId;

    public Map<String, Object> map2=new HashMap<>();

    @Bean
    public Map<String, Object> test2() {

        ("test2 -> | testName -> " + testName + " | test id -> " + testId);
        ("name", testName);
        ("id", testId);
        return map2;
    }
}

三. 如果是默认的 yaml 文件属性,则可以直接使用 @Value

如果使用 yaml 文件可以不指定 properties,同时方式3会覆盖方式2

@Configuration
public class Test3Config {

    @Value("${}")
    private String testName;

    @Value("${}")
    private Integer testId;

    public Map<String, Object> map3=new HashMap<>();

    @Bean
    public Map<String, Object> test2() {

        ("test3 -> | testName -> " + testName + " | test id -> " + testId);
        ("name", testName);
        ("id", testId);
        return map3;
    }
}

四. getter、setter 方法注入及获取配置

@Component
@PropertySource(value = {"classpath:"})
@ConfigurationProperties(prefix="test4")
public class Test4Config {

    private String name;

    private Integer id;

    public String getName() {
        return name;
    }

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

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
         = id;
    }

}
@Autowired
private Test4Config test4Config;

@GetMapping(value = "/test4_config")
public void Test4Config(){
    ("test4 -> | testName -> " + () + " | test id -> " + ());
}

五. 直接读取配置文件

public class Test5Config {

    public static String name;
    public static int id;

    private static String property = "";

    private static Test5Config myConfig;

    static {
        myConfig = loadConfig();
    }

    private static Test5Config loadConfig() {
        if (myConfig == null) {
            myConfig = new Test5Config();
            Properties properties;
            try {
                properties = (property);

                name = ("");
                id = ((""));
            } catch (IOException e) {
                ();
            }
        }
        return myConfig;
    }

    public Test5Config getInstance() {
        return myConfig;
    }

}
@GetMapping(value = "/test5_config")
public void Test5ConfigX(){
    ("test5 -> | testName -> " +  + " | test id -> " + );
}

  • 项目地址