我有一个工厂类,它有各种静态方法来返回某些类的实例。如何在 Spring 中使用不同类中的静态工厂方法创建 bean?
就像是:
public class InstanceFactory
{
public static JellyBean getJellyBeanInstance()
{
return new JellyBean();
}
}
我需要一个果冻豆。
我有一个工厂类,它有各种静态方法来返回某些类的实例。如何在 Spring 中使用不同类中的静态工厂方法创建 bean?
就像是:
public class InstanceFactory
{
public static JellyBean getJellyBeanInstance()
{
return new JellyBean();
}
}
我需要一个果冻豆。
只需将您的getJellyBeanInstance()
方法更改为非静态,然后您需要:
<bean id="instanceFactory" class="InstanceFactory"/>
<bean id="yourBeanId" factory-bean="instanceFactory" factory-method="getJellyBeanInstance"/>
这应该会有所帮助: Spring Bean Instantiation with a static factory method
例如工厂方法,文档中的下一部分应该会有所帮助。