【问题标题】:spring-boot property placeholderspring-boot 属性占位符
【发布时间】:2014-07-16 22:22:03
【问题描述】:

我无法弄清楚为什么我无法在 spring-boot 中将值注入到我的 application.properties 文件中。外部属性到 logging.file 变量中。我有一个如下所示的 application.properties 文件

logging.file=${mylogfile}
server.port=${myport}

对应的Spring-boot应用类

@PropertySources({
@PropertySource("file:///c:/myfolder/externalprops.properties"),
})

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
}
}

和外部属性文件

mylogfile=myoutput.log
myport=8060

当我运行 spring-boot 1.0.2.REL 应用程序时,每次尝试将 mylogfile 注入 application.properties 中的 logging.file 属性时,都会收到以下异常

Exception in thread "main" java.lang.IllegalArgumentException: Could not resolve placeholder 'mylogfile' in string value "${mylogfile}"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)

请注意,如果我自己注入服务器端口号,我在注入和启动应用程序时不会遇到任何问题。

我在这个问题上兜圈子,无法弄清楚我做错了什么。

【问题讨论】:

    标签: properties configuration spring-boot


    【解决方案1】:

    我不认为您可以使用@PropertySource 将值注入“application.properties”——后者必须在读取或什至知道任何@Configuration 之前被解析并准备好使用。您的外部属性可以放在“${user.dir}/application.properties”中,我认为这会实现您想要做的事情。

    【讨论】:

    • 我认为我一定做错了什么。让我感到困惑的是,我能够使用这种方法注入服务器端口。因此,我将头撞在墙上并尝试对 logging.file 做同样的事情的原因。不明白为什么注入端口工作但不注入日志文件
    • 这是关于订单的。必须在创建 ApplicationContext 之前设置日志记录。如果您愿意,您可以从您的应用程序中重新初始化日志记录,并在日志记录开始后切换输出文件(我们认为最好默认执行一次)。
    猜你喜欢
    • 2016-02-22
    • 1970-01-01
    • 2021-03-08
    • 2019-04-15
    • 2017-09-15
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多