弹簧从外部属性文件设置配置文件

时间:2021-05-27 21:59:45

标签: spring-boot spring-boot-configuration

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

在 app.properties 中,如果尝试:spring.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

似乎没有任何效果。

1 个答案:

答案 0 :(得分:0)

如果我没理解错,你的文件在 C 盘上。

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

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