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.
相关推荐
哈哈哈笑什么1 分钟前
3 次生产系统崩溃复盘:Java 后端从踩坑到封神的排查优化之路
java·后端·性能优化
用户3721574261351 分钟前
如何在 Java 中将 RTF 转换为 PDF (含批量转换)
java
ServBay2 分钟前
MongoDB 的文档模型与 CRUD 实战
数据库·后端·mongodb
哈哈哈笑什么5 分钟前
Sleuth+Zipkin 与 OpenSearch 结合是企业级分布式高并发系统的“王炸组合”
分布式·后端·spring cloud
开心猴爷11 分钟前
App HTTPS 抓包实战解析,从代理调试到真实网络流量观察的完整抓包思路
后端
shengjk117 分钟前
为什么按 Ctrl+D 会退出终端?—— 从电传打字机到现代 macOS 的完整旅程
后端
白宇横流学长18 分钟前
基于SpringBoot医院复查开药网站和微信小程序的设计
spring boot·后端·微信小程序
小二·34 分钟前
MyBatis基础入门《十》Spring Boot 整合 MyBatis:从单数据源到多数据源实战
spring boot·后端·mybatis
勇哥java实战分享1 小时前
10GB vs 600MB:我们弃用 GitLab,选择了这个轻量级神器
后端
谷哥的小弟1 小时前
Spring Framework源码解析——ApplicationContextException
java·spring·源码