首页 > 解决方案 > 来自外部属性文件的弹簧组配置文件

问题描述

我正在从外部文件设置我的数据库属性,并尝试对活动配置文件执行相同的操作,但没有任何运气。

如果尝试在 app.propertiesspring.config.location= C:\\run\\secrets\\test中:但这不起作用。

在我尝试过读取数据库属性的同一个配置文件中:

@Component
@Configuration
@PropertySource(value={"file:/run/secrets/file.properties"}, ignoreResourceNotFound = true)
public class AppProperties {

 @Resource
    private Environment env;

@Bean
    public Properties props(){
        Properties props = new Properties();
        props.setProperty("spring.profiles.default", env.getRequiredProperty("spring.profiles.active"));
        //props.put("spring.profiles.active", env.getRequiredProperty("spring.profiles.active"));

        return props;
    }
}

在我的外部文件中,我已经尝试 spring.profiles.active=devspring.profiles.default=dev

似乎没有任何效果。

标签: spring-bootspring-boot-configuration

解决方案


如果我理解正确,您的文件在 C 盘上。

所以,在windows中,外部文件路径应该是这样的:

@PropertySource(value={"file:///C:/run/secrets/file.properties"}, ignoreResourceNotFound = true)

推荐阅读