春季财产占位符不起作用 [英] Spring property placeholder not working

查看:79
本文介绍了春季财产占位符不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在stackoverflow.com上阅读了类似的问题,但是没有一个解决方案对我有帮助. 我使用以下配置(Maven项目结构): src/main/resources/properties/app.properties文件

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

#possible values: dev test prod
mode: dev

在Spring配置中:

In the Spring configuration:

<context:property-placeholder location="classpath:properties/app.properties"/>
<import resource="classpath:/spring/db/${mode}-datasource-config.xml"/>

基于${mode}的值,我要导入相应的数据源配置文件.

Based on the value of ${mode} I want to import the corresponding datasource configuration file.

使用mvn clean install tomcat7:run命令运行嵌入式tomcat7时出现错误:

When I run the embedded tomcat7 using the mvn clean install tomcat7:run command I'm getting the error:

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"

target/classes/properties/app.properties文件存在.

我正在使用IntelliJ IDEA,并且可以在编辑器中单击<import resource="classpath:/spring/db/${mode}-datasource-config.xml"/>中的"$ {mode}",并在属性文件中查看其值.同样,编辑器本身将${mode}更改为灰色的dev,表明它可以识别属性值.在编辑器中,我看到:<import resource="classpath:/spring/db/dev-datasource-config.xml"/>

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"/>

有什么主意为什么会得到我的错误以及如何解决该错误?

Any ideas why I'm getting the error and how it can be resolved?

推荐答案

仅针对环境变量或系统属性来解析导入中的属性占位符.

Property placeholders in imports are resolved against enviroment variables or system properties only.

从3.1版开始,您可以使用ApplicationContextInitializerPropertySources添加到Enviroment中,这将解决您的问题.

Since version 3.1 you can use an ApplicationContextInitializer to add PropertySources to the Enviroment that will solve your problem.

请参见 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/

Other option to do the same is using profiles: http://blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile/

修改

例如:

将初始化程序添加到web.xml

Add the initializer to 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
            }
        }
    } 

这篇关于春季财产占位符不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆