Java spring factory-method 和 factory-bean 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18772490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between spring factory-method and factory-bean?
提问by Sarma
I am new Springs. In Bean tag I found factory-method and factory-bean Attributes. What is the difference between factory-method and factory-bean?
我是新泉。在 Bean 标签中,我找到了 factory-method 和 factory-bean 属性。factory-method 和 factory-bean 有什么区别?
I am using factory-method to call my getInstance static method to create singleton object.
我正在使用工厂方法来调用我的 getInstance 静态方法来创建单例对象。
What is factory-bean used for?
工厂豆是做什么用的?
For given replies, What I understood was
对于给定的答复,我所理解的是
Factory-method is used for calling a static method to create object in same bean class.
工厂方法用于调用静态方法在同一个 bean 类中创建对象。
Factory-bean is used for creating a object based on factory design pattern.
Factory-bean 用于创建基于工厂设计模式的对象。
Ex:- I am asking a EggPlant object from VegetableFactory (Which will return vegetable object which was asked)class by passing my vegetable name(EggPlant in this case).
例如:- 我通过传递我的蔬菜名称(在本例中为 EggPlant)来询问来自VegetableFactory 的EggPlant 对象(它将返回被询问的蔬菜对象)类。
Please correct if I am wrong?
如果我错了请纠正?
采纳答案by Fritz
It's basically the same difference between the Factory methodand Factorydesign 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:
FactoryBean
的接口文档说明:
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.
由 BeanFactory 中使用的对象实现的接口,这些对象本身就是工厂。如果一个 bean 实现了这个接口,它就被用作一个对象暴露的工厂,而不是直接作为一个将暴露自己的 bean 实例。
Also, this object is not used as a bean instance, but as an instance provider through its getObject
method.
此外,此对象不用作 bean 实例,而是通过其getObject
方法用作实例提供者。
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.
搜索factory-method
over a 的使用FactoryBean
,似乎它经常与遗留单例 bean 一起使用,以获取底层实例,但这种方法不提供对初始化方法的支持,例如init
初始化给定集合的方法的属性。
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
.
在这种情况下,您必须在使用类之前自己调用它,定义一个处理初始化的包装器或使用其他机制,例如MethodInvokingFactoryBean
.
Update 2
更新 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.
严格来说, aFactoryBean
旨在管理特定类型。实际上,您将拥有 an EggPlantFactory
,而不是 a ,VegetableFactory
因为接口getObject
定义的方法FactoryBean
不支持参数。
回答by Daniel Kaplan
You can find documentation about this here. See section 3.2.3.2 and 3.2.3.3.
您可以在此处找到有关此的文档。见第 3.2.3.2 和 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.
factory-method
用于指向遗留代码中可用于创建 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.
与使用静态工厂方法创建 bean 非常相似的是使用实例(非静态)工厂方法,其中调用工厂中现有 bean 的工厂方法来创建新 bean。
This is what factory-bean
is for.
这factory-bean
就是为了。
回答by micha
A FactoryBeanis used as factory for other beans/objects. It cannot be used as bean itself.
甲的FactoryBean用作工厂其他豆类/对象。它不能用作 bean 本身。
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.
该factory-method
可用于定义应该用来创建这个bean(而不是调用构造函数)的方法。带有工厂方法的 bean 可以用作 bean 本身。
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.
Spring 可以使用许多不同的工厂类型来创建 bean 实例。如果您想集成绑定到特定类型的库/遗留类,这很有用。
回答by Jagadeesh
factory-method:
As everyone has already explained, factory-methodis used to tell the spring to use the factory method to instantiate the object.
factory-method:
正如大家已经解释过的,factory-method是用来告诉spring使用工厂方法来实例化对象的。
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:
factory-bean:
当没有静态方法并且您仍然想使用非静态方法创建对象时,可以使用factory-bean来完成以下技巧:
<bean id="instanceWithOutStaticMethod" factory-bean="serviceFactory" factory-method="getInstanceWithOutStaticMethod" />
Note: no class attribute required in the above spring definition.
注意:上述 spring 定义中不需要 class 属性。
Class:
班级:
public class ServiceFactory {
public static ServiceFactory getServiceFacrotyWithStaticMethod(){
return new ServiceFactory();
}
public SdCard getInstanceWithOutStaticMethod(){
return new AdaptorSlot("laptop");
}
}
回答by 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
可打印.java
package com.javatpoint;
public interface Printable {
void print();
}
A.java
一个.java
package com.javatpoint;
public class A implements Printable{
@Override
public void print() {
System.out.println("hello a");
}
}
B.java
B.java
package com.javatpoint;
public class B implements Printable{
@Override
public void print() {
System.out.println("hello b");
}
}
PrintableFactory.java
打印工厂.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
<?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
.
请注意,public Printable getPrintable()
ofPrintableFactory.java
是一个非静态方法。通常,如果我们想要访问/调用类的方法或其他资源,我们必须创建它的实例。同样,我们创建了它 bean。使用 bean 的引用变量,pfactory
我们正在调用工厂方法getPrintable
。
回答by 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
在spring中我们使用工厂方法,因为在java中预定义类和一些使用定义类没有提供直接创建其他类的对象有一些抽象类我们不能创建对象但是在类旁边有方法那里像Calendar
类我们不能创建Calendar
类的对象
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
那个时候它给出错误是错误的,但它提供了getInstance()
我们可以调用该方法的方法,我们可以实例化该类
Example:
例子:
Calendar cal=Calendar.getInstance();
it is correct on that time we use factory method in the spring example:
那时我们在 spring 示例中使用工厂方法是正确的:
<bean id="cal" class="Calendar" factory-method="getInstance">