【问题标题】:Load multiple external configuration file in spring boot在spring boot中加载多个外部配置文件
【发布时间】:2018-06-12 14:56:49
【问题描述】:

如何在spring boot中加载多个外部配置属性文件。请在运行 jar 文件时找到以下命令以加载外部属性文件。 “java -jar -Dspring.config.location=myBootProject.jar” 就像我们添加一个或两个配置路径,但是当我们添加两个以上的配置时,我们将如何配置?

【问题讨论】:

  • 你试过“@PropertySources({ @PropertySource("classpath:env1.properties"), @PropertySource("classpath:env2.properties") })”吗?或者您尝试使用“include = env1.properties include = env2.properties”在主属性文件中包含多个属性

标签: spring spring-boot properties-file


【解决方案1】:

在春天你可以这样做:

@Configuration
@PropertySource({
    "classpath:app-config.properties",
    "classpath:dtabase.properties" 
})
public class AppConfig {
    @Autowired
    Environment env;
}

如果您使用的是 spring4 和 Java8 或更高版本:

@Configuration
@PropertySources({
    @PropertySource("classpath:app-config.properties"),
    @PropertySource("classpath:database.properties")
})
public class AppConfig {
    //configuration classes
}

如果属性键重复,最后声明的文件将“获胜”并覆盖。

阅读本文以获取更多信息和完整示例 https://www.mkyong.com/spring/spring-propertysources-example/

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2017-07-25
    • 2015-10-27
    • 1970-01-01
    • 2017-11-26
    • 2017-08-18
    相关资源
    最近更新 更多