Spring IoC容器为您提供了替换bean方法的an option。有人能提供一个使用此功能来解决实际问题的真实示例吗?
我可以看到这是用来改编旧的遗留代码(没有源代码)来与你的应用程序一起工作的。但我认为我会考虑直接使用遗留代码编写适配器类,而不是使用Spring方法替换方法。
发布于 2008-11-11 10:09:58
正如文档所说,它并不是“通常有用”的功能。
不过,它可能有用的一种情况是更改最终类的第三方方法的功能(您不一定拥有源代码)-即其功能不能通过继承修改或扩展的类。
我猜这仍然是一种黑客行为:)
发布于 2008-11-11 09:32:02
现在使用spring IoC,我可以将Lucene Analyzer更改为我想要的任何内容,只需更改一个配置文件即可。
<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");https://stackoverflow.com/questions/280363
复制相似问题