【问题标题】:Spring property placeholder not workingSpring属性占位符不起作用
【发布时间】:2013-05-05 01:49:18
【问题描述】:

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

#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}的值我要导入对应的数据源配置文件。

当我使用 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"

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

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

任何想法为什么我会收到错误以及如何解决它?

【问题讨论】:

  • 您使用哪个 Spring 版本:=3.1?
  • @Ralph, 3.2.2.RELEASE

标签: java spring maven tomcat properties


【解决方案1】:

此属性文件格式有效吗?我认为您应该为app.properties 使用以下内容:

#possible values: dev test prod
mode=dev

【讨论】:

    【解决方案2】:

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

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

    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/

    编辑

    例如:

    将初始化程序添加到 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
                }
            }
        } 
    

    【讨论】:

    • 我在春天不强。我可以在 Spring 配置文件中以声明方式设置 ApplicationContextInitializer 吗?我认为不需要为仅加载属性文件而实现它。找不到任何示例。我只能看到 java 实现。
    • 不,您不能以声明方式进行。 PropertyPlaceHolderConfigurerPropertySourcesPlaceholderConfigurerBeanFactoryPostProcessors 所以导入之前会被解析。
    • @JoseLuisMartin,请告诉我,如何在 Web 环境中基于 java 的配置中设置 ApplicationContextInitializer。当 Disptacher servlet 创建时
    • 对于JavaConfig,看@PropertySource注解
    猜你喜欢
    • 1970-01-01
    • 2014-07-16
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-10
    • 1970-01-01
    相关资源
    最近更新 更多