0

Spring IoC 容器为您提供了替换 bean 方法的选项。有人可以提供使用此功能解决现实生活问题的真实生活示例吗?

我可以看到这用于调整旧的遗留代码(无源代码)以与您的应用程序一起使用。但我想我会考虑直接使用遗留代码而不是 Spring 方法替换方法编写一个适配器类。

4

2 回答 2

1

正如文档所说,它不是“普遍有用的”功能。

A case where it may be useful though is to alter the functionality of a third party method (you don't necessarily have the source) of a final class - i.e. one whose functionality can't be modified or extended through inheritance.

I guess it would still amount to something of a hack though :)

于 2008-11-11T10:09:58.320 回答
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");
于 2008-11-11T09:32:01.970 回答