Spring中的 bean 标签中的 factory-bean , factory-method

1.首先说说 factory-method 是指定创造实例的工厂方法,用法:

factory-method 和 class 配合使用,这时 factory-method 必须是class所指定的类中的一个静态方法,也就是Spring会直接调用 class 所指定的类的静态工厂方法创建一个实例,然后注册到IOC 容器中

factory-method 和 factory-bean 配合使用,factory-bean必须是IOC容器中存在的一个bean实例 beanA,factory-method 配置 factory-bean 所指定的实例 beanA 的一个工厂方法,也就是Spring会先创建 factory-bean 所指定的这个实例 beanA,然后调用beanA的工厂方法,创建一个新的实例 beanB,然后注册到IOC 容器中

以上描述可以看出

1.factory-bean 这个属性和 接口 FactoryBean 没有关系

2.factory-method 必须要和 class 或者 factory-bean 中的一个配合使用,如果class 和 factory-bean都配置了,那么class就不起作用了

复制代码
factory-bean的官方说明:
Alternative to class attribute for factory-method usage.If this is specified, no class attribute should be used.This must be set to the name of a bean in the current or
ancestor factories that contains the relevant factory method.This allows the factory itself to be configured using Dependency Injection, and an instance (rather than static) method to be used.
翻译:使用factory-method时,class属性的替代方案,如果指定了这个(factory-bean),class属性就不用了。factory-bean必须设置一个在当前容器或者父容器中存在的bean,并且这个bean必须拥有factory-method所指定的工厂方法。这种配置方式容许这个工厂bean通过依赖注入进行配置,factory-method 配置的是一个实例方法,不是静态方法
XML 复制代码
<bean id="exampleFactory" class="com.example.ExampleFactory">
    <!-- Inject dependencies here -->
</bean>

<bean id="exampleBean" factory-bean="exampleFactory" factory-method="createInstance"/>
复制代码
factory-method的官方说明:
The name of a factory method to use to create this object. Use constructor-arg elements to specify arguments to the factory method,if it takes arguments. Autowiring does not apply to factory methods.

If the "class" attribute is present, the factory method will be a static method on the class specified by the "class" attribute on this bean definition. Often this will be the same class as that of the constructed object - for example, when the factory method is used as an alternative to a constructor. However, it may be on a different class. In that case, the created object will *not* be of the class specified in the "class" attribute. This is analogous to FactoryBean behavior.

If the "factory-bean" attribute is present, the "class" attribute is not used, and the factory method will be an instance method on the object returned from a getBean call with the specified bean name. The factory bean may be defined as a singleton or a prototype.

The factory method can have any number of arguments. Autowiring is not supported. Use indexed constructor-arg elements in conjunction with the factory-method attribute.

Setter Injection can be used in conjunction with a factory method.Method Injection cannot, as the factory method returns an instance,which will be used when the container creates the bean.
相关推荐
CryptoRzz1 分钟前
印度交易所 BSE 与 NSE 实时数据 API 接入指南
java·c语言·python·区块链·php·maven·symfony
梵得儿SHI4 分钟前
SpringCloud 核心组件精讲:Sentinel 熔断限流全攻略-流量控制、熔断降级、热点参数限流(含 Dashboard 部署 + 项目集成实操)
java·spring cloud·sentinel·熔断降级·热点参数限流·微服务流量控制
麦兜*4 分钟前
Spring Boot 3.x 升级踩坑大全:Jakarta EE 9+、GraalVM Native 与配置迁移实战
java·spring boot·后端·spring·spring cloud
Moment5 分钟前
一杯茶时间带你基于 Yjs 和 reactflow 构建协同流程图编辑器 😍😍😍
前端·后端·面试
看见繁华6 分钟前
GO 教程
开发语言·后端·golang
Yy_Yyyyy_zz9 分钟前
深入理解 Go 的多返回值:语法、编译原理与工程实践
开发语言·后端·golang
独断万古他化10 分钟前
【SpringBoot 配置文件】properties 与 yml 的基础用法、格式及优缺点
java·spring boot·后端
玄〤11 分钟前
Spring MVC 讲解:从初始化流程到请求参数与 JSON 处理全解析(黑马课程ssm笔记总结)(day5)
java·spring·json·mvc
廋到被风吹走14 分钟前
【Java】【JVM】垃圾回收深度解析:G1/ZGC/Shenandoah原理、日志分析与STW优化
java·开发语言·jvm
xrkhy15 分钟前
Java全栈面试题及答案汇总(3)
java·开发语言·面试