0

我有一个属性文件 -

application.properties(内容如下)

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

这是我的弹簧配置 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 应该有效(理论上)。不知道现在该怎么办?

感谢任何指针!

谢谢。

4

1 回答 1

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} 的出现

于 2012-08-30T02:15:43.377 回答