解析spring中的BeanFactory(看完会有收获)

我们常把spring看作一个bean工厂或者ioc容器,它帮助我们负责对象的创建管理,以及对象间依赖关系的建立,还有其他的功能。

关于工厂的实现,一般来说与我们接触最多的就是BeanFactory和ApplicationContext两种实现方式,当然前者是所有实现的父类,ApplicationContext也是继承于它。

BeanFactory,这种方式我们在代码里基本是不会用到的,因为它是面向spring框架本身的,而且提供的功能比较少,而ApplicationContext是面向开发者的,在beanFactory的基础上扩展了不少额外功能,比如事件的监听,再比如对国际化消息的支持,还是很多。

今天就先谈谈BeanFactory

那么我们为什么需要Spring框架来给我们提供这个beanFactory的功能呢?原因是一般我们认为是,可以将原来硬编码的依赖,通过Spring这个beanFactory这个工长来注入依赖,也就是说原来只有依赖方和被依赖方,现在我们引入了第三方——spring这个beanFactory,由它来解决bean之间的依赖问题,达到了松耦合的效果;这个只是原因之一,还有一个更加重要的原因:在没有spring这个beanFactory之前,我们都是直接通过new来实例化各种对象,现在各种对象bean的生产都是通过beanFactory来实例化的,这样的话,spring这个beanFactory就可以在实例化bean的过程中,做一些小动作——在实例化bean的各个阶段进行一些额外的处理,也可以让beanFactory在bean的生命周期的各个阶段中对bean进行各种管理,并且spring将这些阶段通过各种接口暴露给我们,让我们可以对bean进行各种处理,我们只要让bean实现对应的接口,那么spring就会在bean的生命周期调用我们实现的接口来处理该bean。

下面我们看是如何实现这一点的。

1. bean容器的启动

bean实例化必须是在bean容器启动之后。所以就有了两个阶段:

1)bean容器的启动阶段;

2)容器中bean的实例化阶段;

在启动阶段

1> 首先是读取bean的xml配置文件,然后解析xml文件中的各种bean的定义,将xml文件中的每一个<bean />元素分别转换成一个BeanDefinition对象,其中保存了从配置文件中读取到的该bean的各种信息:

public abstract class AbstractBeanDefinition extends BeanMetadataAttributeAccessor
        implements BeanDefinition, Cloneable {
 private volatile Object beanClass;
    private String scope = SCOPE_DEFAULT;
    private boolean abstractFlag = false;
    private boolean lazyInit = false;
    private int autowireMode = AUTOWIRE_NO;
    private int dependencyCheck = DEPENDENCY_CHECK_NONE;
    private String[] dependsOn;private ConstructorArgumentValues constructorArgumentValues;
    private MutablePropertyValues propertyValues;private String factoryBeanName;
    private String factoryMethodName;
    private String initMethodName;
    private String destroyMethodName;

beanClass保存bean的class属性,scop保存bean是否单例,abstractFlag保存该bean是否抽象,lazyInit保存是否延迟初始化,autowireMode保存是否自动装配,dependencyCheck保存是否坚持依赖,dependsOn保存该bean依赖于哪些bean(这些bean必须提取初始化),constructorArgumentValues保存通过构造函数注入的依赖,propertyValues保存通过setter方法注入的依赖,factoryBeanName和factoryMethodName用于factorybean,也就是工厂类型的bean,initMethodName和destroyMethodName分别对应bean的init-method和destory-method属性,比如:

<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">

读完配置文件之后,得到了很多的BeanDefinition对象,

2> 然后通过BeanDefinitionRegistry将这些bean注册到beanFactory中:

public interface BeanDefinitionRegistry extends AliasRegistry {
    void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)throws BeanDefinitionStoreException;
    void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
    BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;
    boolean containsBeanDefinition(String beanName);
    String[] getBeanDefinitionNames();
    int getBeanDefinitionCount();
    boolean isBeanNameInUse(String beanName);
}

BeanFactory的实现类,需要实现BeanDefinitionRegistry 接口,使其具有注册的功能

@SuppressWarnings("serial")
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
        implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
    /** Map of bean definition objects, keyed by bean name */
    private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(64);
    
    @Override
    public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
            throws BeanDefinitionStoreException {
        // ... ...
       this.beanDefinitionMap.put(beanName, beanDefinition);
       // ... ...
    }

我们看到BeanDefinition被注册到了 DefaultListableBeanFactory, 保存在它的一个ConcurrentHashMap中。

将BeanDefinition注册到了beanFactory之后,在这里Spring为我们提供了一个扩展的切口,允许我们通过实现接口BeanFactoryPostProcessor 在此处来插入我们定义的代码:

public interface BeanFactoryPostProcessor {
    
    void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}

bean的实例化阶段

实例化阶段主要是通过反射或者CGLIB对bean进行实例化,在这个阶段Spring又给我们暴露了很多的扩展点:

1> 各种的Aware接口,比如 BeanFactoryAware,MessageSourceAware,ApplicationContextAware

对于实现了这些Aware接口的bean,在实例化bean时Spring会帮我们注入对应的:BeanFactory, MessageSource,ApplicationContext的实例:

public interface BeanFactoryAware extends Aware {
   
    void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}
public interface ApplicationContextAware extends Aware {
   
    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}
public interface MessageSourceAware extends Aware {
   
    void setMessageSource(MessageSource messageSource);
}

2> BeanPostProcessor接口

实现了BeanPostProcessor接口的bean,在实例化bean时Spring会帮我们调用接口中的方法:

public interface BeanPostProcessor {
    /**
     * Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
     * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
     * or a custom init-method). The bean will already be populated with property values.
     * The returned bean instance may be a wrapper around the original.*/
    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
    /**
     * Apply this BeanPostProcessor to the given new bean instance <i>after</i> any bean
     * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
     * or a custom init-method). The bean will already be populated with property values.
     * The returned bean instance may be a wrapper around the original.*/
    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}

从注释中可以知道 postProcessBeforeInitialization方法在 InitializingBean接口的 afterPropertiesSet方法之前执行,而postProcessAfterInitialization方法在 InitializingBean接口的afterPropertiesSet方法之后执行。

3> InitializingBean接口

实现了InitializingBean接口的bean,在实例化bean时Spring会帮我们调用接口中的方法:

public interface InitializingBean {
    /**
     * Invoked by a BeanFactory after it has set all bean properties supplied
     * (and satisfied BeanFactoryAware and ApplicationContextAware).
     * <p>This method allows the bean instance to perform initialization only
     * possible when all bean properties have been set and to throw an
     * exception in the event of misconfiguration.
     * @throws Exception in the event of misconfiguration (such
     * as failure to set an essential property) or if initialization fails.
     */
    void afterPropertiesSet() throws Exception;
}

 4> DisposableBean接口

实现了BeanPostProcessor接口的bean,在该bean死亡时Spring会帮我们调用接口中的方法:

public interface DisposableBean {
    /**
     * Invoked by a BeanFactory on destruction of a singleton.
     * @throws Exception in case of shutdown errors.
     * Exceptions will get logged but not rethrown to allow
     * other beans to release their resources too.
     */
    void destroy() throws Exception;
}

 InitializingBean接口 和 DisposableBean接口对应于 <bean /> 的 init-method 和 destory-method 属性,其经典的例子就是dataSource:

<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">

所以在Spring初始化 dataSource 这个bean之后会调用 DruidDataSource.init 方法:    基本就是初始化数据库连接池。

在dataSource 这个bean死亡时会调用 DruidDataSource.close()方法:    基本就是关闭连接池中的连接

另外注解 @PostConstruct@PreDestroy 也能达到 InitializingBean接口 和 DisposableBean接口的效果。一般来说,这两种是比较常用的,也非常方便

@Service
public class TestImpl{

    @PostConstruct
    public void init(){
        System.out.println("实例化时会调用此方法");
    }
    @PreDestroy
    public void destory(){
        System.out.println("销毁之前会调用此方法");
    }
}

 

 总结

spring容器接管了bean的实例化,不仅仅是通过依赖注入达到了松耦合的效果,同时给我们提供了各种的扩展接口,来在bean的生命周期的各个时期插入我们自己的代码:

0)BeanFactoryPostProcessor接口(在容器启动阶段)

1)各种的Aware接口

2)BeanPostProcessor接口

3)InitializingBean接口(@PostConstruct, init-method)

4)DisposableBean接口(@PreDestroy, destory-method)

posted on 2018-04-17 11:34  半城枫叶半城雨丶  阅读(1083)  评论(0编辑  收藏  举报