java Spring 属性占位符不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16481206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring property placeholder not working
提问by Alexandr
I've read similar issues on the stackoverflow.com, but none of the solutions helped me.
The following configuration I use (maven project structure):
the src/main/resources/properties/app.properties
file
我在 stackoverflow.com 上读过类似的问题,但没有一个解决方案对我有帮助。我使用的配置如下(maven项目结构):src/main/resources/properties/app.properties
文件
#possible values: dev test prod
mode: dev
In the Spring configuration:
在 Spring 配置中:
<context:property-placeholder location="classpath:properties/app.properties"/>
<import resource="classpath:/spring/db/${mode}-datasource-config.xml"/>
Based on the value of ${mode}
I want to import the corresponding datasource configuration file.
根据${mode}
我要导入对应的数据源配置文件的值。
When I run the embedded tomcat7 using the mvn clean install tomcat7:run
command I'm getting the error:
当我使用mvn clean install tomcat7:run
命令运行嵌入式 tomcat7 时,出现错误:
10, 2013 5:52:29 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /SpringWebFlow threw load() exception
java.lang.IllegalArgumentException: Could not resolve placeholder 'mode' in string value "classpath:/spring/db/${mode}-datasource-config.xml"
The target/classes/properties/app.properties
file exists.
该target/classes/properties/app.properties
文件存在。
I'm using IntelliJ IDEA and in the editor I can click on the "${mode}" in <import resource="classpath:/spring/db/${mode}-datasource-config.xml"/>
and see its value in the property file. Also the editor itself change ${mode}
onto the grey colored dev
showing it can recognize the property value. In the editor I see: <import resource="classpath:/spring/db/dev-datasource-config.xml"/>
我正在使用 IntelliJ IDEA,在编辑器中我可以单击“${mode}”<import resource="classpath:/spring/db/${mode}-datasource-config.xml"/>
并在属性文件中查看它的值。此外,编辑器本身更改${mode}
为灰色,dev
表明它可以识别属性值。在编辑器中,我看到:<import resource="classpath:/spring/db/dev-datasource-config.xml"/>
Any ideas why I'm getting the error and how it can be resolved?
任何想法为什么我会收到错误以及如何解决?
回答by Jose Luis Martin
Property placeholders in imports are resolved against enviroment variables or system properties only.
导入中的属性占位符仅针对环境变量或系统属性进行解析。
Since version 3.1 you can use an ApplicationContextInitializer
to add PropertySources
to the Enviroment
that will solve your problem.
从 3.1 版开始,您可以使用 anApplicationContextInitializer
添加PropertySources
到Enviroment
将解决您的问题。
see http://blog.springsource.org/2011/02/15/spring-3-1-m1-unified-property-management/
见http://blog.springsource.org/2011/02/15/spring-3-1-m1-unified-property-management/
Other option to do the same is using profiles: http://blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile/
执行相同操作的其他选项是使用配置文件:http: //blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile/
Edit
编辑
For example:
例如:
Add the initializer to web.xml
将初始化程序添加到 web.xml
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>foo.bar.AppContextInitializer</param-value>
</context-param>
And the initializer:
和初始化程序:
public class AppContextInitializer implements ApplicationContextInitializer<ConfigurableWebApplicationContext> {
@Override
public void initialize(ConfigurableWebApplicationContext applicationContext) {
Properties props;
try {
props = PropertiesLoaderUtils.loadAllProperties("/some/path");
PropertiesPropertySource ps = new PropertiesPropertySource("profile", props);
applicationContext.getEnvironment().getPropertySources().addFirst(ps);
} catch (IOException e) {
// handle error
}
}
}
回答by hoaz
Is this properties file format valid? I think you should use following contents for app.properties
:
此属性文件格式是否有效?我认为你应该使用以下内容app.properties
:
#possible values: dev test prod
mode=dev