tags:

views:

203

answers:

2

Spring IoC container gives you an option of replacing a method of a bean. Can someone provide a real life example of using this feature to solve real life problem?

I can see this used for adapting an old legacy code (w/o sources) to work with your app. But I think I would consider writing an adapter class using the legacy code directly instead of Spring method replacement approach.

A: 

Using spring IoC now I can change my Lucene Analyzers to whatever I want just changing a configuration file.

<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>

and then in the code:

Analyzer analyzer  = (Analyzer) BeanLoader.getFactory().getBean("DocumentAnalyzer");
MrM
I completely missed the point of the question, my example has nothing to do with replacing a method of a bean.
MrM
+1  A: 

As the documentation says, it's not "commonly useful" functionality.

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 :)

Harry Lime
If I need a hack I will try to use delegation for example, but not replace method. I was wondering if someone really used it in as a business code related solution
Georgy Bolyuba