当属性内部具有占位符时,无法解析属性占位符

时间:2012-08-29 19:00:15

标签: spring

我有一个属性文件 -

application.properties(内容如下)

core.microsite=q=MarketId:${marketId}&q=PresaleOff

这是我的spring config xml -

<bean id="myBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="location" value="classpath:/application.properties" />
     </bean>

<bean id="MyQueryBuilder" class="com.search.builder.impl.MyQueryBuilder">
        <property name="queryTemplateMap">
            <map>
                <entry key="microsite" value="${core.microsite}" />
            </map>
        </property>
    </bean>

现在在服务器启动时(tomocat)我得到了这个异常 -

ug 29, 2012 11:50:05 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'MyQueryBuilder' defined in URL [jar:file:/C:/apache-tomcat-6.0.35/webapps/my-service/WEB-INF/lib/my-app-1.0.2.RC8-SNAPSHOT.jar!/META-INF/spring/config/app-context.xml]: Could not resolve placeholder 'marketId'
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:287)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:663)

它明显失败,因为core.microsite属性内部还有一个占位符 $ {marketId}

如何解决此问题?

将“ignoreUnresolvablePlaceholders”设置为true应该有效(理论上)。不知道现在该怎么办?

感谢任何指针!

感谢。

1 个答案:

答案 0 :(得分:1)

我不完全确定错误的根本原因,但可以推荐一些解决方法:

解决方法1 :以这种方式更改占位符的前缀:

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:/application.properties"/>
    <property name="placeholderPrefix" value="%{"></property>
</bean>

<bean id="MyQueryBuilder" class="com.search.builder.impl.MyQueryBuilder">
    <property name="queryTemplateMap">
        <map>
            <entry key="microsite" value="%{core.microsite}" />
        </map>
    </property>
</bean>

解决方法2 :如果您绝对需要将您的房产价值设为$,那么暂时将$ {marketId}更改为其他名称为%{marketId},请@PostConstruct在MyQueryBuilder上查看地图条目,找到并用$ {marketId}替换所有出现的%{marketId}