spring-boot属性占位符

时间:2014-05-27 21:10:46

标签: properties configuration spring-boot

我无法弄清楚为什么我无法在spring-boot中将值注入到我的application.properties文件中。外部属性进入logging.file变量。我有一个application.properties文件,看起来像这样

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

使用相应的Spring-boot Application类

@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)

请注意,如果我自己注入服务器端口号,则注入和启动应用程序时没有任何问题。

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

1 个答案:

答案 0 :(得分:1)

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

相关问题