当前位置:网站首页>【源码解析】BeanFactory和FactoryBean
【源码解析】BeanFactory和FactoryBean
2022-07-31 16:31:00 【华为云】
前言: 作者简介:小明java问道之路,专注于研究计算机底层的博主,就职于金融公司后端高级工程师,擅长交易领域的高安全/可用/并发/性能的设计和架构
Java领域优质创作者、阿里云专家博主、华为云专家
如果此文还不错的话,还请关注、点赞、收藏三连支持一下博主哦
BeanFacotry
The root interface for accessing a Spring bean container.
This is the basic client view of a bean container; further interfaces such as {@link ListableBeanFactory} and {@link org.springframework.beans.factory.config.ConfigurableBeanFactory} are available for specific purposes.
This interface is implemented by objects that hold a number of bean definitions, each uniquely identified by a String name. Depending on the bean definition, the factory will return either an independent instance of a contained object (the Prototype design pattern), or a single shared instance (a superior alternative to the Singleton design pattern, in which the instance is a singleton in the scope of the factory). Which type of instance will be returned depends on the bean factory configuration: the API is the same. Since Spring 2.0, further scopes are available depending on the concrete application context (e.g. "request" and "session" scopes in a web environment).
用于访问SpringBean容器的根接口。
这是bean容器的基本客户端视图;其他接口,如{@link ListableBeanFactory}和{@link org.springframework.beans.factory.config.ConfigurableBeanFactory},可用于特定用途。
该接口由包含多个bean定义的对象实现,每个bean定义由一个字符串名称唯一标识。根据bean定义,工厂将返回包含对象的独立实例(原型设计模式)或单个共享实例(单件设计模式的高级替代方案,其中实例是工厂范围内的单件)。返回哪种类型的实例取决于bean工厂配置:API是相同的。自Spring 2.0以来,根据具体的应用程序上下文(例如web环境中的“请求”和“会话”范围),可以使用更多的范围。
BeanFacotry是spring中比较原始的Factory。如XMLBeanFactory就是一种典型的BeanFactory。原始的BeanFactory无法支持spring的许多插件,如AOP功能、Web应用等。
ApplicationContext接口,它由BeanFactory接口派生而来,ApplicationContext包含BeanFactory的所有功能,通常建议比BeanFactory优先
FactoryBean
Interface to be implemented by objects used within a {@link BeanFactory} which are themselves factories for individual objects. 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.
由{@link BeanFactory}内使用的对象实现的接口,这些对象本身就是单个对象的工厂。如果一个bean实现了这个接口,它将被用作一个对象的工厂来公开,而不是直接作为一个bean实例来公开自己
一般情况下,Spring通过反射机制利用<bean>的class属性指定实现类实例化Bean,在某些情况下,实例化Bean过程比较复杂,如果按照传统的方式,则需要在<bean>中提供大量的配置信息。配置方式的灵活性是受限的,这时采用编码的方式可能会得到一个简单的方案。Spring为此提供了一个org.springframework.bean.factory.FactoryBean的工厂类接口,用户可以通过实现该接口定制实例化Bean的逻辑。FactoryBean接口对于Spring框架来说占用重要的地位,Spring自身就提供了70多个FactoryBean的实现。
边栏推荐
猜你喜欢
二分查找的细节坑
Foreign media right, apple on May be true in inventory
What is the difference between BI software in the domestic market?
研发过程中的文档管理与工具
tooltips使用教程(鼠标悬停时显示提示)
深度学习机器学习理论及应用实战-必备知识点整理分享
i.MX6ULL driver development | 33 - NXP original network device driver reading (LAN8720 PHY)
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
.NET 20周年专访 - 张善友:.NET 技术是如何赋能并改变世界的
你辛辛苦苦写的文章可能不是你的原创
随机推荐
Replication Latency Case (3) - Monotonic Read
2022年必读的12本机器学习书籍推荐
EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
The arm button controls the flashing of the led light (embedded button experiment report)
全新宝马3系上市,安全、舒适一个不落
LevelSequence源码分析
牛客 HJ3 明明的随机数
Flutter gets the height of the status bar statusbar
入职一个月反思
【TypeScript】深入学习TypeScript类型操作
type of timer
字符指针赋值[通俗易懂]
【网络通信三】研华网关Modbus服务设置
2020 WeChat applet decompilation tutorial (can applet decompile source code be used)
ML.NET相关资源整理
Delete table data or clear table
多主复制下处理写冲突(4)-多主复制拓扑
C语言-函数
What is the difference between BI software in the domestic market?
宁波大学NBU IT项目管理期末考试知识点整理