2

I have a factory class which has various static methods to return the instances of some classes. How can a bean be created in spring using static factory method in different class?

something like:

public class InstanceFactory
{    
  public static JellyBean getJellyBeanInstance()
  {
    return new JellyBean(); 
  }
}

I need a JellyBean.

supertonsky
  • 2,563
  • 6
  • 38
  • 68
varun
  • 684
  • 1
  • 11
  • 30

2 Answers2

4

Just change your getJellyBeanInstance() method to non-static, then you need:

<bean id="instanceFactory" class="InstanceFactory"/>

<bean id="yourBeanId" factory-bean="instanceFactory" factory-method="getJellyBeanInstance"/>
MattR
  • 6,908
  • 2
  • 21
  • 30
1

This should help: Spring Bean Instantiation with a static factory method

For instance factory method, next section from the document should help.

Vic
  • 21,473
  • 11
  • 76
  • 97
Atul
  • 2,673
  • 2
  • 28
  • 34
  • I already know how it can be done if I create bean of my factory class, but i dont want to create any instance of my factory class. Lets say my factory class is abstract. – varun Dec 10 '13 at 11:53