2008-11-11 14 views
0

Spring IoC容器給你更換一個bean的方法an option。有人可以提供一個真實生活中使用此功能來解決現實生活問題的例子嗎?現實生活的商業案例,使用彈簧的方法替換?

我可以看到這個用於適應舊的遺留代碼(W/O源)與應用工作。但我想我會考慮使用傳統的代碼,而不是直接的春天方法替代方法寫一個適配器類。

回答

1

由於文件說,這不是 「很常用」 功能。

儘管可能有用的一種情況是改變最終類的第三方方法(不一定有源代碼)的功能 - 即其功能無法通過繼承進行修改或擴展的方法。

我猜它仍然會達到一個黑客的東西雖然:)

+0

如果我需要一個黑客,我會嘗試使用委派例如,而不是替代方法。我想知道是否有人真的使用它作爲業務代碼相關的解決方案 – 2008-11-11 15:54:23

0

使用Spring IoC現在我可以改變我的Lucene的分析儀到任何我想做的只是改變一個配置文件。

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
       <property name="locations"> 
         <list> 
           <value>file.properties</value> 
         </list> 
       </property> 
</bean> 


<bean id="DocumentAnalyzer" class="${lucene.document_analyzer}"> 
</bean> 

<bean id="QueryAnalyzer" class="${lucene.query_analyzer}"> 
</bean> 

<bean id="IndexSearcher" class="org.apache.lucene.search.IndexSearcher" scope="prototype"> 
    <constructor-arg> 
    <value>${lucene.repository_path}</value> 
    </constructor-arg> 

</bean> 

,然後在代碼:

Analyzer analyzer = (Analyzer) BeanLoader.getFactory().getBean("DocumentAnalyzer"); 
+0

我完全錯過了問題的要點,我的例子無關與更換一個bean的方法。 – MrM 2008-11-11 10:33:51