Spring源码之FactoryBean的实现
https://zhuanlan.zhihu.com/p/97005407
https://blog.csdn.net/qq_35634181/article/details/104507465
总结
- FactoryBean的对象会先在createWebServer时,创建BeanWrapper,并放到factoryBeanInstanceCache缓存中
- 在预实例化时,走FactoryBean逻辑,调用getBean去执行属性注入、初始化过程(此时对象实例化直接从factoryBeanInstanceCache缓存中取)
- 完成FactoryBean的实现类初始化
- 调用实现类的getObject方法,从而对工厂中类进行功能增强
FactoryBean(实现类)的初始化
初始化入口依然是AbstractApplicationContext#finishBeanFactoryInitialization() ,走到DefaultListableBeanFactory类preInstantiateSingletons方法时,和普通bean逻辑不同,执行以下代码:
@Override
public void preInstantiateSingletons() throws BeansException {
// Iterate over a copy to allow for init methods which in turn register new bean definitions.
// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
// Trigger initialization of all non-lazy singleton beans...
for (String beanName : beanNames) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
//FactoryBean的初始化执行入口
if (isFactoryBean(beanName)) {
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
if (bean instanceof FactoryBean) {
FactoryBean<?> factory = (FactoryBean<?>) bean;
boolean isEagerInit;
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
isEagerInit = AccessController.doPrivileged(
(PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit,
getAccessControlContext());
}
else {
isEagerInit = (factory instanceof SmartFactoryBean &&
((SmartFactoryBean<?>) factory).isEagerInit());
}
if (isEagerInit) {
getBean(beanName);
}
}
}
else {
//普通bean的执行入口
getBean(beanName);
}
}
}
}
调用Object bean = getBean(FACTORY_BEAN_PREFIX + beanName),后面基本和普通bean的初始化一致
其中不同有:instanceWrapper不为null,是createWebServer时放入factoryBeanInstanceCache中的
if (mbd.isSingleton()) {
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
将FactoryBean实现类放入缓存调用链为:
SpringApplication#run() --> SpringApplication#refreshContext() --> SpringApplication#refresh() -->ServletWebServerApplicationContext#refresh() --> AbstractApplicationContext#refresh() --> AbstractApplicationContext#onRefresh() --> ServletWebServerApplicationContext#createWebServer() --> ServletWebServerApplicationContext#getWebServerFactory() --> DefaultListableBeanFactory#getBeanNamesForType() --> DefaultListableBeanFactory#doGetBeanNamesForType() --> AbstractBeanFactory#isTypeMatch() --> AbstractAutowireCapableBeanFactory#getTypeForFactoryBean() --> AbstractAutowireCapableBeanFactory#getSingletonFactoryBeanForTypeCheck()
mbd为:
// Generics potentially only match on the target class, not on the proxy...
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
bw为:
Object instance;
try {
// Mark this bean as currently in creation, even if just partially.
beforeSingletonCreation(beanName);
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
instance = resolveBeforeInstantiation(beanName, mbd);
if (instance == null) {
bw = createBeanInstance(beanName, mbd, null);
instance = bw.getWrappedInstance();
}
}
catch (BeanCreationException ex) {
......
}
finally {
// Finished partial creation of this bean.
afterSingletonCreation(beanName);
}
FactoryBean为:
FactoryBean<?> fb = getFactoryBean(beanName, instance);
if (bw != null) {
this.factoryBeanInstanceCache.put(beanName, bw);
}
获取FactoryBean实现类(非工厂)的过程
如(此时beanName前面没有&):
Object factoryBeanTest = context.getBean("factoryBeanTest");
会先在AbstractBeanFactory#doGetBean()中执行:
protected <T> T doGetBean(
String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
throws BeansException {
// Eagerly check singleton cache for manually registered singletons.
Object sharedInstance = getSingleton(beanName);
if (sharedInstance != null && args == null) {
......
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
}
执行getObjectForBeanInstance方法:
protected Object getObjectForBeanInstance(
Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {
......
Object object = null;
if (mbd != null) {
mbd.isFactoryBean = true;
}
else {
object = getCachedObjectForFactoryBean(beanName);
}
if (object == null) {
// Return bean instance from factory.
FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
// Caches object obtained from FactoryBean if it is a singleton.
if (mbd == null && containsBeanDefinition(beanName)) {
mbd = getMergedLocalBeanDefinition(beanName);
}
boolean synthetic = (mbd != null && mbd.isSynthetic());
object = getObjectFromFactoryBean(factory, beanName, !synthetic);
}
return object;
}
调用getObjectFromFactoryBean,再调用doGetObjectFromFactoryBean方法:
protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
if (factory.isSingleton() && containsSingleton(beanName)) {
synchronized (getSingletonMutex()) {
//object为null
Object object = this.factoryBeanObjectCache.get(beanName);
if (object == null) {
object = doGetObjectFromFactoryBean(factory, beanName);
......
}
}
}
}
最后调用实际FactoryBean实现类的getObject方法,完成工厂类的自定义功能增强
private Object doGetObjectFromFactoryBean(FactoryBean<?> factory, String beanName) throws BeanCreationException {
Object object;
try {
if (System.getSecurityManager() != null) {
......
}
else {
//去执行实际实现类的getObject方法
object = factory.getObject();
}
}
return object;
}
通过FactoryBean.getObject方式
如:
FactoryBean bean = (FactoryBean)context.getBean("&factoryBeanTest");
CustomTask customTask = (CustomTask)bean.getObject();
会通过AbstractBeanFactory#doGetBean()直接返回bean
protected <T> T doGetBean(
String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
throws BeansException {
// Eagerly check singleton cache for manually registered singletons.
//此时一级缓存singletonObjects中有数据
Object sharedInstance = getSingleton(beanName);
if (sharedInstance != null && args == null) {
......
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
调用getObjectForBeanInstance,返回beanInstance
protected Object getObjectForBeanInstance(
Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {
// Don't let calling code try to dereference the factory if the bean isn't a factory.
//此时为true
if (BeanFactoryUtils.isFactoryDereference(name)) {
......
if (mbd != null) {
mbd.isFactoryBean = true;
}
return beanInstance;
}
}
其后便是调用FactoryBean实现类的getObject方法,完成工厂类的自定义功能增强
FactoryBean实现示例:
@Component
public class FactoryBeanTest implements FactoryBean {
@Override
public Object getObject() throws Exception {
//一些自定义功能
......
return new CustomTask();
}
@Override
public Class<?> getObjectType() {
return CustomTask.class;
}
}
springboot中调用例子:
@RestController
public class TestController {
@Autowired
private AnnotationConfigServletWebServerApplicationContext context;
@RequestMapping("/test")
public String test() throws Exception {
Object factoryBeanTest = context.getBean("factoryBeanTest");
System.out.println(factoryBeanTest.toString());
FactoryBean bean = (FactoryBean)context.getBean("&factoryBeanTest");
CustomTask customTask = (CustomTask)bean.getObject();
customTask.execute();
return bean.getObject().toString();
}
}
Spring源码之FactoryBean的实现的更多相关文章
- Spring源码学习
Spring源码学习--ClassPathXmlApplicationContext(一) spring源码学习--FileSystemXmlApplicationContext(二) spring源 ...
- Spring源码学习之:FactoryBean的使用
转载:http://book.51cto.com/art/201311/419081.htm ==========个人理解========================= FactoryBean和B ...
- 【Spring源码分析】原型Bean实例化过程、byName与byType及FactoryBean获取Bean源码实现
原型Bean加载过程 之前的文章,分析了非懒加载的单例Bean整个加载过程,除了非懒加载的单例Bean之外,Spring中还有一种Bean就是原型(Prototype)的Bean,看一下定义方式: & ...
- spring源码分析系列 (8) FactoryBean工厂类机制
更多文章点击--spring源码分析系列 1.FactoryBean设计目的以及使用 2.FactoryBean工厂类机制运行机制分析 1.FactoryBean设计目的以及使用 FactoryBea ...
- Spring源码分析(十二)FactoryBean的使用
摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 一般情况下,Spring通过反射机制利用bean的class属性指定实现 ...
- 【spring源码系列】之【FactoryBean类型的接口】
1.概述 目前我们知道,spring创建bean有多种方式,比如xml方式创建,比如@Component,@Service,@Controler,@Repository注解创建,比如@Autowire ...
- spring源码:学习线索(li)
一.spring xml配置(不包括AOP,主要了解在初始化及实例化过程中spring配置文件中每项内容的具体实现过程,从根本上掌握spring) <bean>的名字 &,alia ...
- Spring源码分析——BeanFactory体系之抽象类、类分析(二)
上一篇分析了BeanFactory体系的2个类,SimpleAliasRegistry和DefaultSingletonBeanRegistry——Spring源码分析——BeanFactory体系之 ...
- spring源码分析(一)IoC、DI
创建日期:2016.08.06 修改日期:2016.08.07 - 2016.08.12 交流QQ:992591601 参考书籍:<spring源码深度解析>.<spring技术内幕 ...
随机推荐
- 【ST表】SCOI2016 萌萌哒
题目内容 洛谷链接 一个长度为\(n\)的大数,用\(S_1S_2S_3...S_n\)表示,其中\(S_i\)表示数的第\(i\)位,\(S_1\)是数的最高位,告诉你一些限制条件,每个条件表示为四 ...
- IE下文件上传, SCRIPT5: 拒绝访问 问题
最近遇到一个比较奇葩的问题,某些ie浏览器在页面中上传文件时,无法上传.查看控制台报错: SCRIPT5: 拒绝访问. jquery-3.2.1.min.js, 行4 字符5725 .并且我的最新版I ...
- nginx安全:配置allow/deny控制ip访问(ngx_http_access_module)
一,nginx中allow/deny指令的用途 1, Nginx的deny和allow指令是由ngx_http_access_module模块提供, Nginx安装默认内置了该模块 2, nginx访 ...
- Windows(WSL2) Linux子系统搭建Docker环境
摘要:本文主要介绍了如何再Windows(WSL2)中启用Linux系统中,并搭建Docker环境. WSL是适用于 Linux 的 Windows 子系统可让开发人员按原样运行 GNU/Linux ...
- 在windows2003上安装itunes
本人使用windows server 2003系统 安装itunes时提示 AppleMobileDeviceSupport 只能按照在xp系统上或以上版本,你可以忽略这个错误.继续安装吧. 这样除了 ...
- 主流开源分布式图数据库 Benchmark
本文由美团 NLP 团队高辰.赵登昌撰写 首发于 Nebula Graph 官方论坛:https://discuss.nebula-graph.com.cn/t/topic/1377 1. 前言 近年 ...
- docker 启动容器restart 策略
docker 运行容器时使用--restart 参数可以指定一个restart策略,来指定容器应该如何重启,或不应该重启,当容器启用restart策略时,将会载docker ps 显示up 或者res ...
- poj 2229 一道动态规划思维题
http://poj.org/problem?id=2229 先把题目连接发上.题目的意思就是: 把n拆分为2的幂相加的形式,问有多少种拆分方法. 看了大佬的完全背包代码很久都没懂,就照着网上的写了动 ...
- Distcp 跨集群同步
date: 2020-10-09 17:45:00 updated: 2020-10-10 17:45:00 Distcp 跨集群同步 1. 使用方法及原理 hadoop distcp srcPath ...
- 一些常见JAVA问题
原文:https://blog.csdn.net/weiyongxuan/article/details/45920765 一.Java的异常的基类是java.lang.Throwable 二.守护线 ...