2012-08-29 158 views
0

我已經一個屬性文件 -無法解析性佔位符,當屬性內部有一個佔位符

application.properties(如下內容)

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

這裏是我的Spring配置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應該起作用(理論上)。不知道現在該做什麼?

欣賞任何指針!

謝謝。

+0

在applicationContext中使用'$ {core.microsite}'時,是否希望'$ {marketId}'替代例如'q = MarketId:12345&q = PresaleOff',或保留爲'q = MarketId:$ {marketId}&q = PresaleOff'? – yorkw

+0

我希望該值保持不變。那就是 - q = MarketId:$ {marketId}&q = PresaleOff – Rishi

+0

嘗試在屬性文件'core.microsite = q = MarketId:\ $ \ {marketId \}&q = PresaleOff'中使用轉義字符。 – yorkw

回答

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},在您的MyQueryBuilder上有一個@PostConstruct,您可以查看地圖條目並查找並更換%{marketId}與$ {marketId}的所有匹配