2011-01-12 112 views
2

我想用spring設置一個屬性值。使用ref-bean設置spring bean屬性值

<bean id="velocityPropsBean" class="com.test.CustomProperties" abstract="false" singleton="true" lazy-init="false" autowire="default" dependency-check="default"> 
    <property name="properties"> 
    <props> 
     <prop key="resource.loader">file</prop> 
     <prop key="file.resource.loader.cache">true</prop> 
     <prop key="file.resource.loader.class">org.apache.velocity.runtime.resource.loader.FileResourceLoader</prop> 
     <prop key="file.resource.loader.path">NEED TO INSERT VALUE AT STARTUP</prop> 

    </props> 

    </property> 
</bean> 

<bean id="velocityResourcePath" class="java.lang.String" factory-bean="velocityHelper" factory-method="getLoaderPath"/> 

現在我需要做的是將getLoaderPath的結果插入到file.resource.loader.path中。 getLoaderPath的值發生變化,所以必須在服務器啓動時加載。

任何想法如何將velocityResourcePath值插入屬性?

回答

4

使用Spring 3,你可以跳過中間階段,直接調用使用SpringEL工廠:

<prop key="file.resource.loader.path">#{ velocityHelper.loaderPath }</prop> 

或許

<prop key="file.resource.loader.path">#{ velocityHelper.getLoaderPath() }</prop> 

這將讓你刪除velocityResourcePath豆。

+0

感謝您的回答。我試過了,並得到以下錯誤。我正在使用spring 2.0 - org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:來自類路徑資源[velocity-context.xml]的XML文檔中的第17行無效;嵌套異常是org.xml.sax.SAXParseException:元素類型「prop」的內容必須匹配「null」。 原因:org.xml.sax.SAXParseException:元素類型「prop」的內容必須匹配「null」。 – Pushkar 2011-01-13 00:50:03

1

下面的代碼可能會對您有所幫助。

<import resource="classpath:/DaoContext.xml"/> 

<bean id="ClientMasterDao" class="dao.hibernate.impl.ClientMasterImpl"> 
<property name="sessionFactory" ref="sessionFactory"/> 
</bean> 

<bean id="ClientMasterServices" class="client.ClientMasterServices"> 
<property name="clientDao" ref="ClientMasterDao"/> 
</bean> 
相關問題