【问题标题】:loading spring-{env}.properties in IntelliJ 2016.3在 IntelliJ 2016.3 中加载 spring-{env}.properties
【发布时间】:2017-05-04 01:35:54
【问题描述】:

我有一个包含三个属性文件的 Spring Boot 属性:

我有一个使用 application-local.properties 文件中定义的值的类,我认为它会被使用,因为 application-properties 有`spring.profiles.active=local

当我在 IntelliJ 中启动时,我在控制台日志中看到了这一点:

Loaded config file 'file:.../build/resources/main/application.properties' (classpath:/application.properties)
2016-12-18 20:10:17.596 DEBUG 29999 --- [  restartedMain] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file 'file:.../build/resources/main/application-local.properties' (classpath:/application-local.properties)`
Skipped (empty) config file 'file:...build/resources/main/application-local.properties' (classpath:/application-local.properties) for profile local

它说它跳过了空配置文件,但是当我检查“build/resources/main/application-local.properties”时,我看到了其中的值。

但是,当我在 IntelliJ 中将以下类作为 Spring Boot 应用程序运行时,它没有这些属性。它抛出一个空指针,因为它们是空的:

@Value("${myapp.ApiKey}")
private String apiKey;

@Value("${myapp.ApiSecret}")
private String apiSecret;

@Value("${myapp.user}")
private String user;

在应用程序配置中,我尝试使用活动配置文件、程序参数和环境变量。

但是,如果我从命令行运行:

java -jar -Dspring.active.profiles=local  build/libs/myjar-0.0.1-SNAPSHOT.jar

然后它工作得很好。我确定这以前可以正常工作,但不确定 IntelliJ 版本是否相同。有关如何使用正确版本的 application.properties 的任何提示?

【问题讨论】:

    标签: intellij-idea spring-boot


    【解决方案1】:

    您使用的是哪个 Spring Boot 版本?如果您使用的是最新版本,则应该是

    -Dspring.profiles.active=local

    http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-set-active-spring-profiles

    【讨论】:

    • 我已经将它添加到 IntelliJ 并且它没有被拾取。如何让 IntelliJ 查看正确的属性文件?
    • 你的问题恰恰相反。首先,您使用了错误的标志,java -jar -Dspring.active.profiles=localjava -jar -Dspring.profiles.active=local。其次,我认为您不需要将 Spring 指向您的属性文件,因为您已经为它添加了配置文件名称的后缀,因此 Spring 应该会自动选择它。如果您仍然对如何执行此操作感到好奇,尽管您可以使用此标志 -Dspring.config.location=/path/to/file.properties 指定属性文件的位置
    • 那么在你的application.properties 文件上你设置了spring.profiles.active=local?
    • 你是对的,因为我必须使用旧 (?) 标志。但我想知道为什么我必须声明这一点 - application.properties 文件正在加载,但它没有指向正确的 env 文件(application-local.properties)所以这些值是 null 除非我通过命令提供它行或 IntelliJ 环境设置。我还注意到实际上只有一个 @Configuration 类被加载。
    猜你喜欢
    • 2010-12-22
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    • 2017-10-03
    • 2019-03-14
    • 2017-04-22
    • 2020-12-02
    • 1970-01-01
    相关资源
    最近更新 更多