Properties spring启动属性占位符

Properties spring启动属性占位符,properties,configuration,spring-boot,Properties,Configuration,Spring Boot,我无法理解为什么无法在spring boot中将值注入到application.properties文件中。将外部属性导入logging.file变量。我有一个application.properties文件,如下所示 logging.file=${mylogfile} server.port=${myport} 使用相应的Spring启动应用程序类 @PropertySources({ @PropertySource("file:///c:/myfolder/externalprops.pr

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

logging.file=${mylogfile}
server.port=${myport}
使用相应的Spring启动应用程序类

@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)
请注意,如果我自己注入服务器端口号,那么注入和启动应用程序不会有任何问题


我在这个问题上兜圈子,不知道我做错了什么。

我认为你不能使用
@PropertySource
将值注入“application.properties”——后者必须经过解析,在读取任何
@Configuration
之前就可以使用了。您的外部属性可以放在“${user.dir}/application.properties”中,我认为这将实现您正在尝试的操作。

我认为我必须做一些错误的事情。让我感到困惑的是,我能够使用这种方法注入服务器端口。因此,我将头撞在墙上,并尝试对logging.file执行同样的操作。不明白为什么注入端口工作,但不注入日志文件这是关于订单的。必须在创建
ApplicationContext
之前设置日志记录。如果您愿意,您可以从应用程序中重新初始化日志记录,并在日志记录开始后切换输出文件(我们认为默认情况下最好只执行一次)。