https://zhuanlan.zhihu.com/p/97005407

https://blog.csdn.net/qq_35634181/article/details/104507465

总结

  1. FactoryBean的对象会先在createWebServer时,创建BeanWrapper,并放到factoryBeanInstanceCache缓存中
  2. 在预实例化时,走FactoryBean逻辑,调用getBean去执行属性注入、初始化过程(此时对象实例化直接从factoryBeanInstanceCache缓存中取)
  3. 完成FactoryBean的实现类初始化
  4. 调用实现类的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的实现的更多相关文章

  1. Spring源码学习

    Spring源码学习--ClassPathXmlApplicationContext(一) spring源码学习--FileSystemXmlApplicationContext(二) spring源 ...

  2. Spring源码学习之:FactoryBean的使用

    转载:http://book.51cto.com/art/201311/419081.htm ==========个人理解========================= FactoryBean和B ...

  3. 【Spring源码分析】原型Bean实例化过程、byName与byType及FactoryBean获取Bean源码实现

    原型Bean加载过程 之前的文章,分析了非懒加载的单例Bean整个加载过程,除了非懒加载的单例Bean之外,Spring中还有一种Bean就是原型(Prototype)的Bean,看一下定义方式: & ...

  4. spring源码分析系列 (8) FactoryBean工厂类机制

    更多文章点击--spring源码分析系列 1.FactoryBean设计目的以及使用 2.FactoryBean工厂类机制运行机制分析 1.FactoryBean设计目的以及使用 FactoryBea ...

  5. Spring源码分析(十二)FactoryBean的使用

    摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 一般情况下,Spring通过反射机制利用bean的class属性指定实现 ...

  6. 【spring源码系列】之【FactoryBean类型的接口】

    1.概述 目前我们知道,spring创建bean有多种方式,比如xml方式创建,比如@Component,@Service,@Controler,@Repository注解创建,比如@Autowire ...

  7. spring源码:学习线索(li)

    一.spring xml配置(不包括AOP,主要了解在初始化及实例化过程中spring配置文件中每项内容的具体实现过程,从根本上掌握spring) <bean>的名字 &,alia ...

  8. Spring源码分析——BeanFactory体系之抽象类、类分析(二)

    上一篇分析了BeanFactory体系的2个类,SimpleAliasRegistry和DefaultSingletonBeanRegistry——Spring源码分析——BeanFactory体系之 ...

  9. spring源码分析(一)IoC、DI

    创建日期:2016.08.06 修改日期:2016.08.07 - 2016.08.12 交流QQ:992591601 参考书籍:<spring源码深度解析>.<spring技术内幕 ...

随机推荐

  1. 题解:CF593D Happy Tree Party

    题解:CF593D Happy Tree Party Description Bogdan has a birthday today and mom gave him a tree consistin ...

  2. python中jsonpath模块运用

    原文链接:https://www.cnblogs.com/denise1108/p/10265911.html 1. jsonpath介绍用来解析多层嵌套的json数据;JsonPath 是一种信息抽 ...

  3. 接口管理平台Yapi

    1.介绍 YApi 是由去哪儿移动架构组推出的一款开源项目,是高效.易用.功能强大的 api 管理平台,旨在为开发.产品.测试人员提供更优雅的接口管理服务. 官网:https://yapi.ymfe. ...

  4. pycharm2018.3.5 下载激活(windows平台)

    软件下载: 百度网盘下载 提取码: 73p7 激活操作: 1.下载jar包 JetbrainsCrack-4.2-release-enc.jar 链接:https://pan.baidu.com/s/ ...

  5. 链接WPA2-企业WIFI时出现无法链接到该网络,可以链接个人WIFI时的问题和解决方案

    因在一个问题上掉两次坑所以还是决定记录下来,方便以后查阅. 第一次因为要部署.net 应用程序要求使用TLS1.2,所以修改了操作系统的默认启用的安全协议类型,导致好多应用程序出问题. 第二次因为vs ...

  6. 【C++】 C++异常捕捉和处理

    在阅读别人开发的项目中,也许你会经常看到了多处使用异常的代码,也许你也很少遇见使用异常处理的代码.那在什么时候该使用异常,又在什么时候不该使用异常呢?在学习完异常基本概念和语法之后,后面会有讲解. ( ...

  7. c++ 遍历目录下文件、文件夹

    BOOL GetDirFiles(const char* pszDir, char* pszFileType, std::vector<std::string>& vtFileLi ...

  8. hystrix线程池隔离的原理与验证

    引子 幸福很简单: 今天项目半年规划被通过,终于可以早点下班.先坐公交,全程开着灯,买了了几天的书竟然有时间看了.半小时后,公交到站,换乘大巴车.车还等着上人的功夫,有昏暗的灯光,可以继续看会儿书.过 ...

  9. docker部署nginx服务器

    1,下载nginx镜像 docker pull nginx 2,启动 docker run --name runoob-nginx-test -p 8081:80 -d nginx 3,创建本地目录 ...

  10. Linux部署常用命令

    1../startup.sh 开启tomcat服务 2.rm -f file1  删除文件 3.rmdir dir xxx 删除目录 4.Tab按钮 自动补全文件名称功能 5.gzip file1  ...