What is the difference between spring factory-method and factory-bean?

邮差的信 提交于 2019-12-02 20:27:20

It's basically the same difference between the Factory method and Factory design patterns, with a little note at the bottom. While one is a method used to obtain instances of a specific class, the other is a full fledged object responsible of creating objects, including all of the required logic to do so.

FactoryBean's interface documentation states:

Interface to be implemented by objects used within a BeanFactory which are themselves factories. If a bean implements this interface, it is used as a factory for an object to expose, not directly as a bean instance that will be exposed itself.

Also, this object is not used as a bean instance, but as an instance provider through its getObject method.


Update

Searching for uses of factory-method over a FactoryBean, it seems that it used quite oftenly with legacy singleton beans, to get the underlying instance, but this approach doesn't provide support for initialization methods, such as, for example, an init method that initializes a given set of properties.

In this case, you either have to invoke it yourself before using the class, define a wrapper that handles the initialization or make use of other mechanisms such as MethodInvokingFactoryBean.


Update 2

Strictly speaking, a FactoryBean is intended to manage a specific type. You'd have, in fact, an EggPlantFactory, not a VegetableFactory since the getObject method defined by the FactoryBean interface doesn't support parameters.

Achilles Ram Nakirekanti
factory-method: represents the factory method that will be invoked to inject the bean.
factory-bean: represents the reference of the bean by which factory method will be invoked. It is used if factory method is non-static.

Printable.java

package com.javatpoint;  
public interface Printable {  
    void print();  
}  

A.java

package com.javatpoint;  
public class A implements Printable{  
    @Override  
    public void print() {  
        System.out.println("hello a");  
    }      
} 

B.java

package com.javatpoint;  
public class B implements Printable{  
    @Override  
    public void print() {  
        System.out.println("hello b");  
    }  
}  

PrintableFactory.java

package com.javatpoint;  
public class PrintableFactory {  
    //non-static factory method  
    public Printable getPrintable(){  
        return new A();//return any one instance, either A or B  
    }  
}  

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  

<bean id="pfactory" class="com.javatpoint.PrintableFactory"></bean>  
<bean id="p" class="com.javatpoint.Printable" factory-method="getPrintable"   
factory-bean="pfactory"></bean>  

</beans>  

Notice that public Printable getPrintable() of PrintableFactory.java is a non-static method. Generally if we want access/call method or other resources of the a class we have to create it's instance. Similarly in that we created it bean. using bean's reference variable pfactory we are calling the factory method getPrintable.

Jagadeesh

factory-method:
As everyone has already explained, factory-method is used to tell the spring to use the factory method to instantiate the object.

Note: static method is mandatory else throws an exception.

<bean id="serviceFactory" class="com.test.controller.ServiceFactory" factory-method="getServiceFacrotyWithStaticMethod"/>

factory-bean:
When there is no static method and still you want to create the object using non static method, here is the trick can be done using factory-bean:

<bean id="instanceWithOutStaticMethod" factory-bean="serviceFactory" factory-method="getInstanceWithOutStaticMethod" />

Note: no class attribute required in the above spring definition.

Class:

public class ServiceFactory {

    public static ServiceFactory getServiceFacrotyWithStaticMethod(){
        return new ServiceFactory();
    }

    public SdCard getInstanceWithOutStaticMethod(){
        return new AdaptorSlot("laptop");
    }
}

You can find documentation about this here. See section 3.2.3.2 and 3.2.3.3.

factory-method is used to point to a static method in legacy code that can be used to create a bean.

Quite similar to using a static factory method to create a bean, is the use of an instance (non-static) factory method, where a factory method of an existing bean from the factory is called to create the new bean.

This is what factory-bean is for.

A FactoryBean is used as factory for other beans/objects. It cannot be used as bean itself.

The factory-method can be used to define the method that should be used to create this bean (instead of calling the constructor). A bean with a factory method can be used as a bean itself.

Spring can create bean instances using many different factory types. This is useful if you want to integrate libraries/legacy classes that are bound to a particular type.

Hemant vyas

in spring we use factory-method because in java predefine class and some use define class are there the are not provide direct to create the object of other class there are some abstract class we can not create object but in side the class there are method are there like Calendar class we can not create the object of Calendar class

Calendar cal = new Calendar(); 

it is wrong on that thime its give error but it is provide the method getInstance() we can call the method and we can instance the class

Example:

Calendar cal=Calendar.getInstance();  

it is correct on that time we use factory method in the spring example:

<bean id="cal" class="Calendar" factory-method="getInstance">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!