我在 stackoverflow.com 上读过类似的问题,但没有一个解决方案对我有帮助。
我使用的配置如下(maven项目结构):
这src/main/resources/properties/app.properties
file
#possible values: dev test prod
mode: dev
在Spring配置中:
<context:property-placeholder location="classpath:properties/app.properties"/>
<import resource="classpath:/spring/db/${mode}-datasource-config.xml"/>
基于的价值${mode}
我想导入相应的数据源配置文件。
当我使用以下命令运行嵌入式 tomcat7 时mvn clean install tomcat7:run
命令我收到错误:
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
文件已存在。
我正在使用 IntelliJ IDEA,在编辑器中我可以单击“${mode}”<import resource="classpath:/spring/db/${mode}-datasource-config.xml"/>
并在属性文件中查看其值。编辑器本身也发生变化${mode}
到灰色的颜色上dev
显示它可以识别财产价值。在编辑器中我看到:<import resource="classpath:/spring/db/dev-datasource-config.xml"/>
有什么想法为什么我会收到错误以及如何解决它吗?
导入中的属性占位符仅根据环境变量或系统属性进行解析。
从 3.1 版本开始你可以使用ApplicationContextInitializer
to add PropertySources
to the 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/
执行相同操作的其他选项是使用配置文件: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
例如:
将初始化程序添加到 web.xml
<context-param>
<param-name>contextInitializerClasses</param-name>
<param-value>foo.bar.AppContextInitializer</param-value>
</context-param>
和初始化器:
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
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)