代码改变世界

聊聊Spring的FactoryBean其实没那么难

2020-12-15 09:16  爱研究源码的javaer  阅读(512)  评论(3编辑  收藏  举报

前言

谈到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原理