spring factory-method和factory-bean有什么区别?

Sar*_*rma 23 java spring spring-mvc springsource spring-social

我是新的泉.在Bean标签中,我找到了factory-method和factory-bean属性.factory-method和factory-bean有什么区别?

我使用factory-method来调用我的getInstance静态方法来创建singleton对象.

什么是工厂豆用于?


对于给定的回复,我理解的是

Factory-method用于调用静态方法以在同一个bean类中创建对象.

Factory-bean用于根据工厂设计模式创建对象.

例如: - 我通过传递我的蔬菜名称(在这种情况下为EggPlant)从VegetableFactory(将返回被要求的蔬菜对象)类中询问一个EggPlant对象.

如果我错了请纠正?

小智 17

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.
Run Code Online (Sandbox Code Playgroud)

Printable.java

package com.javatpoint;  
public interface Printable {  
    void print();  
}  
Run Code Online (Sandbox Code Playgroud)

A.java

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

B.java

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

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  
    }  
}  
Run Code Online (Sandbox Code Playgroud)

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>  
Run Code Online (Sandbox Code Playgroud)

请注意public Printable getPrintable()PrintableFactory.java是一个非静态方法.通常,如果我们想要访问/调用方法或类的其他资源,我们必须创建它的实例.同样,我们创建了bean.使用bean的引用变量,pfactory我们调用工厂方法getPrintable.


Fri*_*itz 11

这与Factory方法Factory设计模式之间的差异基本相同,底部有一点注释.虽然一个是用于获取特定类的实例的方法,但另一个是负责创建对象的完整对象,包括执行此操作所需的所有逻辑.

FactoryBean的界面文档说明:

由BeanFactory中使用的对象实现的接口,这些对象本身就是工厂.如果bean实现了这个接口,它将被用作公开的对象的工厂,而不是直接作为将自己公开的bean实例.

此外,此对象不用作bean实例,而是通过其getObject方法用作实例提供程序.


更新

搜索factory-methodover的使用FactoryBean,似乎它经常使用传统的单例bean来获取底层实例,但是这种方法不支持初始化方法,例如init,初始化给定集合的方法属性.

在这种情况下,您必须在使用类之前自己调用它,定义处理初始化的包装器或使用其他机制,例如MethodInvokingFactoryBean.


更新2

严格来说,a FactoryBean旨在管理特定类型.事实上,你有一个EggPlantFactory,而不是VegetableFactory因为接口getObject定义的方法FactoryBean不支持参数.


Jag*_*esh 10

factory-method:
正如大家已经解释的那样,factory-method用于告诉spring使用factory方法来实例化对象.

注意:静态方法是必需的,否则抛出异常.

<bean id="serviceFactory" class="com.test.controller.ServiceFactory" factory-method="getServiceFacrotyWithStaticMethod"/>
Run Code Online (Sandbox Code Playgroud)

factory-bean:
当没有静态方法但仍然想要使用非静态方法创建对象时,可以使用factory-bean完成这个技巧:

<bean id="instanceWithOutStaticMethod" factory-bean="serviceFactory" factory-method="getInstanceWithOutStaticMethod" />
Run Code Online (Sandbox Code Playgroud)

注意:上面的spring定义中不需要class属性.

类:

public class ServiceFactory {

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

    public SdCard getInstanceWithOutStaticMethod(){
        return new AdaptorSlot("laptop");
    }
}
Run Code Online (Sandbox Code Playgroud)