spring factory-method和factory-bean有什么区别? [英] What is the difference between spring factory-method and factory-bean?

查看:628
本文介绍了spring factory-method和factory-bean有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

I am using factory-method to call my getInstance static method to create singleton object.

什么是工厂bean用于?

What is factory-bean used for?

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

For given replies, What I understood was

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

Factory-method is used for calling a static method to create object in same bean class.

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

Factory-bean is used for creating a object based on factory design pattern.

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

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).

如果我错了,请更正吗?

Please correct if I am wrong?

推荐答案

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

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 的接口文档说明:


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

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.

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

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

搜索工厂的用途 - 方法通过 FactoryBean ,它似乎经常用于传统的单例bean,以获取底层实例,但这种方法不提供支持初始化方法,例如初始化给定属性集的 init 方法。

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.

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

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.

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

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.

这篇关于spring factory-method和factory-bean有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆