前言

谈到Spring的FactoryBean,就会知道Spring中经典的面试题:FactoryBean和BeanFactory的区别。我们这里就简单概括下: 、

  1. BeanFactory是接口,提供了OC容器最基本的形式,给具体的IOC容器的实现提供了规范,FactoryBean也是接口,为IOC容器中Bean的实现提供了更加灵活的方式,FactoryBean在IOC容器的基础上给Bean的实现加上了一个简单工厂模式和装饰模式(如果想了解装饰模式参考:修饰者模式(装饰者模式,Decoration) 我们可以在getObject()方法中灵活配置。其实在Spring源码中有很多FactoryBean的实现类.

  2. BeanFactory是个Factory,也就是IOC容器或对象工厂,FactoryBean是个Bean。在Spring中,所有的Bean都是由BeanFactory(也就是IOC容器)来进行管理的。但对FactoryBean而言,这个Bean不是简单的Bean,而是一个能生产或者修饰对象生成的工厂Bean,它的实现与设计模式中的工厂模式和修饰器模式类似

示例

首先我们定义个普通的java pojo对象名叫Car

public class Car {

   private   int maxSpeed ;
private String brand ;
private double price ; public double getPrice() {
return price;
} public int getMaxSpeed() {
return maxSpeed;
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
} public void setPrice(double price) {
this.price = price;
} @Override
public String toString() {
return "Car{" +
"maxSpeed=" + maxSpeed +
", brand='" + brand + '\'' +
", price=" + price +
'}';
}
}

再定义一个实现FactoryBean接口的实现类CarFactoryBean

public class CarFactoryBean  implements FactoryBean<Car> {
private String carInfo; public Car getObject() throws Exception { System.out.println("start new car");
Car car = new Car();
String[] infos = carInfo.split(",");
car.setBrand(infos[0]);
car.setMaxSpeed(Integer.valueOf(infos[1]));
car.setPrice(Double.valueOf(infos[2]));
return car;
} public Class<Car> getObjectType(){
return Car.class ; } //返回的实例是同一个
public boolean isSingleton() {
return true;
} public String getCarInfo() {
return this.carInfo ;
} public void setCarInfo(String carInfo) {
this.carInfo = carInfo;
}
}

再来看看我们的Xml配置是如何配置Bean的

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="car" name="car1 car2" class="com.john.factorybean.CarFactoryBean">
<property name="carInfo" value="超级跑车,400,2000000"/>
</bean>
</beans>

最后我们加载这个配置文件,启动Spring容器来获取car这个Bean

public static void main( String[] args ) {
ClassPathXmlApplicationContext ac =new ClassPathXmlApplicationContext();
ac.setConfigLocations("spring-config-locatorfactory.xml");
ac.refresh();
//getBean的时候才会去getObject
ac.getBean("car", Car.class);
}

成功打印出

start new car
Car{maxSpeed=400, brand='超级跑车', price=2000000.0}

原理

如果我们把ac.getBean("car", Car.class);这行代码去掉,我们会发现start new car这行是不会打印的,那就是说明getObject是延迟加载的,只有显示调用才能触发,spring在初始化启动单例bean实例化的时候并不会触发(SmartFactoryBean除外,之后我们会单独分析下SmartFactoryBean)

那我们就到源码级别证明下Spring的FactoryBean的getObject是延迟初始化的

DefaultListableBeanFactory

preInstantiateSingletons

@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...
//todo 遍历所有不是延迟初始化的 单例 bean 2020-08-28
for (String beanName : beanNames) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
//不是抽象的 且是单例的 且不是懒加载的
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
//todo 如果是factorybean 那还得去判断 是不是eagerInit
if (isFactoryBean(beanName)) {
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
if (bean instanceof FactoryBean) {
final 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 {
getBean(beanName);
}
}
}
}

我们看到Spring会在实例化单例Bean的时候会去判断是否是Factorybean,如果是的话就会去根据FACTORY_BEAN_PREFIX + beanName获取Factorybean本身,然后再判断这个FactoryBean是SmartFactoryBean且isEagerInit设置为true才会去获取beanName对应的Bean,也就是实例化这个bean。

那既然是延迟初始化的,那我们再获取实际bean的时候Spring内部是怎么做的呢?

AbstractBeanFactory

doGetBean

protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException { // Create bean instance.
//从头开始加载 bean,这个过程由 getSingleton() 实现。
if (mbd.isSingleton()) {
//https://www.cnblogs.com/leihuazhe/p/9481018.html
////todo 如果有单例那直接返回了 , args参数也就没啥用处了 2020-08-30
sharedInstance = getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly(显式) remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
});
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
}

这里我们从getObjectForBeanInstance方法可以看出来这里就是获取真正bean的方法。

getObjectForBeanInstance

protected Object getObjectForBeanInstance(
Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) { // Now we have the bean instance, which may be a normal bean or a FactoryBean.
// If it's a FactoryBean, we use it to create a bean instance, unless the
// caller actually wants a reference to the factory.
//我们使用它来创建一个bean实例,除非调用者实际上想要引用工厂
//todo 如果是获取FactoryBean本身 那就直接返回这个工厂
if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
return beanInstance;
}
}

也就是说不是FactoryBean本身或者根据name判断就是获取FactoryBean本身的话,那就直接返回这个beanInstance。

那我们在getBean("car")的时候其实获取到的beanInstance是FactoryBean的实例,所以走到这边的时候不会返回。

下面Spring就会走到抽象类FactoryBeanRegistrySupport中的getObjectFromFactoryBean函数,因为AbstractBeanFactory继承自FactoryBeanRegistrySupport,从名字就可以看出这个函数的意义就是调用FactoryBean的getObject方法。

FactoryBeanRegistrySupport

getObjectFromFactoryBean

/**
* Obtain an object to expose from the given FactoryBean.
* @param factory the FactoryBean instance
* @param beanName the name of the bean
* @param shouldPostProcess whether the bean is subject to post-processing
* @return the object obtained from the FactoryBean
* @throws BeanCreationException if FactoryBean object creation failed
* @see org.springframework.beans.factory.FactoryBean#getObject()
*/
protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) { if (factory.isSingleton() && containsSingleton(beanName)) {
synchronized (getSingletonMutex()) {
Object object = this.factoryBeanObjectCache.get(beanName);
if (object == null) {
object = doGetObjectFromFactoryBean(factory, beanName);
// Only post-process and store if not put there already during getObject() call above
// (e.g. because of circular reference processing triggered by custom getBean calls)
Object alreadyThere = this.factoryBeanObjectCache.get(beanName);
if (alreadyThere != null) {
object = alreadyThere;
}
else {
if (shouldPostProcess) {
if (isSingletonCurrentlyInCreation(beanName)) {
// Temporarily(暂时) return non-post-processed object, not storing it yet..
return object;
}
beforeSingletonCreation(beanName);
try {
object = postProcessObjectFromFactoryBean(object, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(beanName,
"Post-processing of FactoryBean's singleton object failed", ex);
}
finally {
afterSingletonCreation(beanName);
}
}
//object放入factoryBeanObjectCache缓存
if (containsSingleton(beanName)) {
this.factoryBeanObjectCache.put(beanName, object);
}
}
}
return object;
}
}
else {
//非单例
省略部分代码
}
}

函数内部还会调用doGetObjectFromFactoryBean函数

doGetObjectFromFactoryBean

/**
* Obtain an object to expose from the given FactoryBean.
* @param factory the FactoryBean instance
* @param beanName the name of the bean
* @return the object obtained from the FactoryBean
* @throws BeanCreationException if FactoryBean object creation failed
* @see org.springframework.beans.factory.FactoryBean#getObject()
*/
private Object doGetObjectFromFactoryBean(final FactoryBean<?> factory, final String beanName)
throws BeanCreationException { Object object;
try {
if (System.getSecurityManager() != null) {
AccessControlContext acc = getAccessControlContext();
try {
object = AccessController.doPrivileged((PrivilegedExceptionAction<Object>) factory::getObject, acc);
}
catch (PrivilegedActionException pae) {
throw pae.getException();
}
}
else {
//最重要的调用getObject方法
object = factory.getObject();
}
}
catch (FactoryBeanNotInitializedException ex) {
throw new BeanCurrentlyInCreationException(beanName, ex.toString());
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);
} // Do not accept a null value for a FactoryBean that's not fully
// initialized yet: Many FactoryBeans just return null then.
if (object == null) {
if (isSingletonCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(
beanName, "FactoryBean which is currently in creation returned null from getObject");
}
object = new NullBean();
}
return object;
}

从这个函数中我们看到了FactoryBean自身的getObject方法。调用完getObject方法后,Spring就会把这个对象放入factoryBeanObjectCache缓存对象中。之后再获取的时候直接从factoryBeanObjectCache缓存中拿就是了。

聊聊Spring的FactoryBean其实没那么难的更多相关文章

  1. block没那么难(二):block和变量的内存管理

    本系列博文总结自<Pro Multithreading and Memory Management for iOS and OS X with ARC> 了解了 block的实现,我们接着 ...

  2. .NET C#到Java没那么难,MVC篇

    最典型的JAVA MVC就是JSP + servlet + javabean的模式.比较好的MVC,老牌的有Struts.Webwork.新兴的MVC 框架有Spring MVC.Tapestry.J ...

  3. 从架构演进的角度聊聊Spring Cloud都做了些什么?

    Spring Cloud作为一套微服务治理的框架,几乎考虑到了微服务治理的方方面面,之前也写过一些关于Spring Cloud文章,主要偏重各组件的使用,本次分享主要解答这两个问题:Spring Cl ...

  4. 聊聊 Spring Boot 2.x 那些事儿

    本文目录: 即将的 Spring 2.0 - Spring 2.0 是什么 - 开发环境和 IDE - 使用 Spring Initializr 快速入门 Starter 组件 - Web:REST ...

  5. 没那么难,谈CSS的设计模式

    没那么难,谈CSS的设计模式 来源: 灵感的小窝  发布时间: 2016-09-09 16:46  阅读: 8949 次  推荐: 27   原文链接   [收藏]   什么是设计模式? 曾有人调侃, ...

  6. 【小家Spring】聊聊Spring中的数据绑定 --- BeanWrapper以及内省Introspector和PropertyDescriptor

    #### 每篇一句 > 千古以来要饭的没有要早饭的,知道为什么吗? #### 相关阅读 [[小家Spring]聊聊Spring中的数据转换:Converter.ConversionService ...

  7. 【小家Spring】聊聊Spring中的数据绑定 --- DataBinder本尊(源码分析)

    每篇一句 唯有热爱和坚持,才能让你在程序人生中屹立不倒,切忌跟风什么语言或就学什么去~ 相关阅读 [小家Spring]聊聊Spring中的数据绑定 --- 属性访问器PropertyAccessor和 ...

  8. Spring事务专题(五)聊聊Spring事务到底是如何实现的

    前言 本专题大纲: 本文为本专题倒数第二篇文章. 在上篇文章中我们一起学习了Spring中的事务抽象机制以及动手模拟了一下Spring中的事务管理机制,那么本文我们就通过源码来分析一下Spring中的 ...

  9. block没那么难(一):block的实现

    本系列博文总结自<Pro Multithreading and Memory Management for iOS and OS X with ARC> block 顾名思义就是代码块,将 ...

随机推荐

  1. SpringBoot加载配置文件的几种方式

    首先回忆一下在没有使用SpringBoot之前也就是传统的spring项目中是如何读取配置文件,通过I/O流读取指定路径的配置文件,然后再去获取指定的配置信息. 传统项目读取配置方式 读取xml配置文 ...

  2. Java面试那些事

    网址链接:https://mp.weixin.qq.com/s/BkiDwyjua4iwws7gWHwK9Q 引言 其实本来真的没打算写这篇文章,主要是LZ得记忆力不是很好,不像一些记忆力强的人,面试 ...

  3. 莫小安 Linux下Redis的安装与配置

    转载自--Linux下Redis的安装与配置 redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcached类似,但很大程度补偿了 memcached的不足,它 ...

  4. cakephp中sql查询大于

    $list = $this->Capital->find('all', array('conditions'=>array('amount >'=>0)));

  5. 深度分析:Java 静态方法/变量,非静态方法/变量的区别,今天一并帮你解决!

    静态/非静态 方法/变量的写法 大家应该都明白静态方法/字段比普通方法/字段的写法要多一个static关键字,简单写下他们的写法吧,了解的可以直接略过 class Test{ // 静态变量 publ ...

  6. mac用户怎么保护自己的隐私安全?

    使用过Windows系统的小伙伴们应该都知道,Windows系统下有360电脑管家和腾讯电脑管家等几款著名清理软件,专门用于清理电脑缓存.垃圾文件以及清除浏览痕迹,这对于Windows用户是大大节省了 ...

  7. GIF图保存下来不会动?用Folx的浏览器捕获下载功能试试!

    表情包大多是GIF格式的动图,有时候使用浏览器的另存为保存完发现并不能动态播放,怎么办呢?试试Folx提供的浏览器捕获下载功能,就能将各种格式的图片,包括GIF动图的下载链接捕获下来,供进一步下载使用 ...

  8. 【PYTEST】第三章参数化

    知识点: 参数化 1. parametrize() 参数化测试用例 实际工作中,我们不可能就传一组数据,参数化支持多组数据测试,并且每组都会去执行,parametrize(), 第一个参数用逗号开分隔 ...

  9. HarmonyOS Java UI之DependentLayout布局示例

    DependentLayout简介 DependentLayout意为相对位置布局,与DirectionalLayout相比较有更多的排布方式,每个组件可以指定相对于其他同级组件的位置,也可以指定相对 ...

  10. 软件工程与UML第一次作业

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE2/ 这个作业要求在哪里 https://edu.cnblogs.com/campus/f ...