The road so far….

May 10, 2010

Use Spring FactoryBean for your factories

Filed under: java — Tags: — Rahul Sharma @ 10:12 pm

Spring has been the cornerstone of many applications.  It offers many gems that we employ in our current day-to-day activities eg PropertyPlaceHolderConfigurer, used to load properties, Resource, can be used for loading files/streams from any location etc. But besides these most popular ones Spring also offers some hidden ones that are not out-of-the box components but yet quite useful ones. Here I will explain FactoryBean/ObjectFactory in Spring that can be used to implement factories.


Spring employs the concept of FactoryBean to create beans from your XML configuration. You can use the same concept to create factories that can be employed to create objects that can be injected through DI.  Here is an example :

class Store{
    Collection selectedProducts;
     // some more logic

interface Product{
        // attributes of product

Now  we have a Store that has a bunch of products. In order to give it products we will create a ProductFactory.

class ProductFactory{
   Collection getAllProducts(){
        // create the collection of products

The store requires products that can be given by ProductFactory, so you can inject it into the store and then use the init-method argument to initialize the product list through Spring Bean life-cycle.

class StoreWithPF{
   Collection<Product> products;
   ProductFactory pf;
   public void initialize(){
   } }

XML definition:

<bean id="store" class="StoreWithPF".......... init-method="initialize"
< bean id="productFactory"...../>

But now the Store contains the logic of creating the Products using the factory, or we can abstract this somewhere else but still this plumbing  remains to be written. Here the Spring FactoryBean comes to rescue. Let me show how..

class SpringBasedProductFactory implements FactoryBean{
public Object getObject(){
   // create and return the products

public Class getObjectType(){
   return Collection.class;

XML Definations:

<bean id="store" class="Store" p:products-ref="productsFactory"/>
<bean id="productsFactory" class="springBasesProductFactory" />

See the difference here, the product collection in store refers to the productsFactory. Now when spring creates the store Bean it looks for the productsFactory to inject the products collection. It sees it as FactoryBean and calls the getObject method to get the list of products and sets it in the store product collection. Now  no additional code is required to coordinate the behaviour of injecting products from the factory to the store.

The ObjectFactory interface works just like the FactoryBean interface. It is  meant to be used as API for creating objects and is the most relevent candidate for creating factories rather the FactoryBean. One added benefit of using it  is we have to implement less methods.



  1. […] This post was mentioned on Twitter by Richard Laksana, Thomas Hansen Gøytil. Thomas Hansen Gøytil said: How to create factories using #spring FactoryBean: […]

    Pingback by Tweets that mention Use Spring FactoryBean for your factories | The road so far…. -- — May 13, 2010 @ 3:30 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: