December 19, 2014
Hot Topics:

Using Method Injection with a Spring Factory, Page 2

  • April 4, 2011
  • By Shekhar Gulati
  • Send Email »
  • More Articles »

Using Spring ServiceLocatorFactoryBean

Using method injection along with a factory was a better approach than using a simple factory, but I still have to write all the conditional code (i.e. if the report type is GIF then give me GifReader and so on). A much better solution is to use ServiceLocatorFactoryBean, which is a FactoryBean implementation that takes an interface that must have one or more methods with the signature MyService getService() or MyService getService(String id). It creates a dynamic proxy, which implements the interface using standard Java classes , delegating to an underlying BeanFactory.

Using ServiceLocatorFactoryBean ,I just need to define a service locator interface called ReadersFactory, which has one method getReader that takes a String argument as imageType.

public interface ReadersFactory {

public ImageReader getReader(ImageType imageType);

}</code></pre>
<p>An XML context config file will look like this:</p>
<pre><code><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
default-autowire="byName">

<bean id="service" class="com.shekhar.service.ImageService">
<constructor-arg>
<ref bean="imageServiceFactory" />
</constructor-arg>
</bean>

<bean id="jpegReader" class="com.shekhar.reader.JpegReader" scope="prototype" />

<bean id="gifReader" class="com.shekhar.reader.GifReader" scope="prototype" />

<bean id="defaultReader" class="com.shekhar.reader.DefaultReader"
scope="prototype" />

<bean id="imageServiceFactory"
class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean">
<property name="serviceLocatorInterface" value="com.shekhar.reader.ReadersFactory"></property>
<property name="serviceMappings">
<props>
<prop key="JPEG">
jpegReader
</prop>
<prop key="GIF">
gifReader
</prop>
</props>
</property>
</bean><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
default-autowire="byName">

<bean id="service" class="com.shekhar.service.ImageService">
<constructor-arg>
<ref bean="imageServiceFactory" />
</constructor-arg>
</bean>

<bean id="jpegReader" class="com.shekhar.reader.JpegReader" scope="prototype" />

<bean id="gifReader" class="com.shekhar.reader.GifReader" scope="prototype" />

<bean id="defaultReader" class="com.shekhar.reader.DefaultReader"
scope="prototype" />

<bean id="imageServiceFactory"
class="org.springframework.beans.factory.config.ServiceLocatorFactoryBean">
<property name="serviceLocatorInterface" value="com.shekhar.reader.ReadersFactory"></property>
<property name="serviceMappings">
<props>
<prop key="JPEG">
jpegReader
</prop>
<prop key="GIF">
gifReader
</prop>
</props>
</property>
</bean>
</beans>
</beans>

I defined a bean ServiceLocatorFactoryBean and provide it the serviceLocatorInterface, which in this case is ReadersFactory. After that, I injected a properties object that contains the mapping of key (to the look up the bean name for example). When ReadersFactory receives an argument with ImageType as "GIF", the gifReader bean will be returned.

With this approach, I can use the power of ServiceLocatorFactoryBean to define conditional factories in Spring XML without any of the usual method injection limitations.

About the Author

Shekhar Gulati -- Contributing Editor, Java -- is a Java consultant with over 5 years experience. He currently works with Xebia India, an Agile Software Development company. The opinions in this article and on his blog are his own and do not necessarily represent the opinions of his employer. His own blog is at and you can follow him on twitter here.


Tags: XML, Spring

Originally published on http://www.developer.com.

Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel