深入剖析 Spring 框架的 BeanFactory


說到Spring框架,人們往往大談特談一些似乎高逼格的東西,比如依賴注入,控制反轉,面向切面等等。但是卻忘記了最基本的一點,Spring的本質是一個bean工廠(beanFactory)或者說bean容器,它按照我們的要求,生產我們需要的各種各樣的bean,提供給我們使用。只是在生產bean的過程中,需要解決bean之間的依賴問題,才引入了依賴注入(DI)這種技術。也就是說依賴注入是beanFactory生產bean時為了解決bean之間的依賴的一種技術而已。

那么我們為什么需要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方法注入的依賴factoryBeanNamefactoryMethodName用於factorybean,也就是工廠類型的bean,initMethodNamedestroyMethodName分別對應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 {
    /**
     * Modify the application context's internal bean factory after its standard
     * initialization. All bean definitions will have been loaded, but no beans
     * will have been instantiated yet. This allows for overriding or adding
     * properties even to eager-initializing beans.
     * @param beanFactory the bean factory used by the application context
     * @throws org.springframework.beans.BeansException in case of errors
     */
    void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}

典型的例子就是:PropertyPlaceholderConfigurer,我們一般在配置數據庫的dataSource時使用到的占位符的值,就是它注入進去的:

public abstract class PropertyResourceConfigurer extends PropertiesLoaderSupport
        implements BeanFactoryPostProcessor, PriorityOrdered {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        try {
            Properties mergedProps = mergeProperties();
            // Convert the merged properties, if necessary.
            convertProperties(mergedProps);
            // Let the subclass process the properties.
            processProperties(beanFactory, mergedProps);
        }
        catch (IOException ex) {
            throw new BeanInitializationException("Could not load properties", ex);
        }
    }
processProperties(beanFactory, mergedProps);在子類中實現的,功能就是將
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="url" value="${jdbc_url}" />
        <property name="username" value="${jdbc_username}" />
        <property name="password" value="${jdbc_password}" />
${jdbc_username}等等這些替換成實際值。

bean的實例化階段

實例化階段主要是通過反射或者CGLIB對bean進行實例化,在這個階段Spring又給我們暴露了很多的擴展點:

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

對於實現了這些Aware接口的bean,在實例化bean時Spring會幫我們注入對應的:BeanFactory, MessageSource,ApplicationContext的實例:

public interface BeanFactoryAware extends Aware {
    /**
     * Callback that supplies the owning factory to a bean instance.
     * <p>Invoked after the population of normal bean properties
     * but before an initialization callback such as
     * {@link InitializingBean#afterPropertiesSet()} or a custom init-method.
     * @param beanFactory owning BeanFactory (never {@code null}).
     * The bean can immediately call methods on the factory.
     * @throws BeansException in case of initialization errors
     * @see BeanInitializationException
     */
    void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}
public interface ApplicationContextAware extends Aware {
    /**
     * Set the ApplicationContext that this object runs in.
     * Normally this call will be used to initialize the object.
     * <p>Invoked after population of normal bean properties but before an init callback such
     * as {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}
     * or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
     * {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
     * {@link MessageSourceAware}, if applicable.
     * @param applicationContext the ApplicationContext object to be used by this object
     * @throws ApplicationContextException in case of context initialization errors
     * @throws BeansException if thrown by application context methods
     * @see org.springframework.beans.factory.BeanInitializationException
     */
    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}
public interface MessageSourceAware extends Aware {
    /**
     * Set the MessageSource that this object runs in.
     * <p>Invoked after population of normal bean properties but before an init
     * callback like InitializingBean's afterPropertiesSet or a custom init-method.
     * Invoked before ApplicationContextAware's setApplicationContext.
     * @param messageSource message sourceto be used by this object
     */
    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 方法:

    public void init() throws SQLException {
        // ... ...try {
            lock.lockInterruptibly();
        } catch (InterruptedException e) {
            throw new SQLException("interrupt", e);
        }
        boolean init = false;
        try {  
            connections = new DruidConnectionHolder[maxActive];
            SQLException connectError = null;
            try {                
                for (int i = 0, size = getInitialSize(); i < size; ++i) {
                    Connection conn = createPhysicalConnection();
                    DruidConnectionHolder holder = new DruidConnectionHolder(this, conn);
                    connections[poolingCount++] = holder;
                }
                if (poolingCount > 0) {
                    poolingPeak = poolingCount;
                    poolingPeakTime = System.currentTimeMillis();
                }
            } catch (SQLException ex) {
                LOG.error("init datasource error", ex);
                connectError = ex;
            }          
        } catch (SQLException e) {
            LOG.error("dataSource init error", e);
            throw e;
        } catch (InterruptedException e) {
            throw new SQLException(e.getMessage(), e);
        } finally {
            inited = true;
            lock.unlock();
        }
    }

基本就是初始化數據庫連接池。

在dataSource 這個bean死亡時會調用 DruidDataSource.close()方法:

   public void close() {
        lock.lock();
        try {
          for (int i = 0; i < poolingCount; ++i) {
                try {
                    DruidConnectionHolder connHolder = connections[i];
                    for (PreparedStatementHolder stmtHolder : connHolder.getStatementPool().getMap().values()) {
                        connHolder.getStatementPool().closeRemovedStatement(stmtHolder);
                    }
                    connHolder.getStatementPool().getMap().clear();
                    Connection physicalConnection = connHolder.getConnection();
                    physicalConnection.close();
                    connections[i] = null;
                    destroyCount.incrementAndGet();
                } catch (Exception ex) {
                    LOG.warn("close connection error", ex);
                }
            }          
        } finally {
            lock.unlock();
        }
    }

基本就是關閉連接池中的連接。

另外注解 @PostConstruct 和 @PreDestroy 也能達到 InitializingBean接口 和 DisposableBean接口的效果。

2. 總結

spring容器接管了bean的實例化,不僅僅是通過依賴注入達到了松耦合的效果,同時給我們提供了各種的擴展接口,來在bean的生命周期的各個時期插入我們自己的代碼:

0)BeanFactoryPostProcessor接口(在容器啟動階段)

1)各種的Aware接口

2)BeanPostProcessor接口

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

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

3. FactoryBean接口

實現了FactoryBean接口的bean是一類叫做factory的bean。其特點是,spring會在使用getBean()調用獲得該bean時,會自動調用該bean的getObject()方法,所以返回的不是factory這個bean,而是這個bean.getOjbect()方法的返回值:

public interface FactoryBean<T> {
    T getObject() throws Exception;
    Class<?> getObjectType();
    boolean isSingleton();
}

典型的例子有spring與mybatis的結合:

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="configLocation" value="classpath:config/mybatis-config-master.xml" />
      <property name="mapperLocations" value="classpath*:config/mappers/master/**/*.xml" />
    </bean>

我們看上面該bean,因為實現了FactoryBean接口,所以返回的不是 SqlSessionFactoryBean 的實例,而是她的 SqlSessionFactoryBean.getObject() 的返回值

public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory>, InitializingBean, ApplicationListener<ApplicationEvent> {

  private static final Log logger = LogFactory.getLog(SqlSessionFactoryBean.class);

  private Resource configLocation;

  private Resource[] mapperLocations;

  private DataSource dataSource;
  
  public SqlSessionFactory getObject() throws Exception {
    if (this.sqlSessionFactory == null) {
      afterPropertiesSet();
    }

    return this.sqlSessionFactory;
  }

其實他是一個專門生產 sqlSessionFactory 的工廠,所以才叫 SqlSessionFactoryBean。 而SqlSessionFactory又是生產SqlSession的工廠。

還有spring與ibatis的結合:

    <!-- Spring提供的iBatis的SqlMap配置 -->
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation" value="classpath:sqlmap/sqlmap-config.xml" />
        <property name="dataSource" ref="dataSource" />
    </bean>
public class SqlMapClientFactoryBean implements FactoryBean<SqlMapClient>, InitializingBean {
    private Resource[] configLocations;
    private Resource[] mappingLocations;
    private Properties sqlMapClientProperties;
    private DataSource dataSource;
    private boolean useTransactionAwareDataSource = true;
    private Class transactionConfigClass = ExternalTransactionConfig.class;
    private Properties transactionConfigProperties;
    private LobHandler lobHandler;
    private SqlMapClient sqlMapClient;
    public SqlMapClient getObject() {
        return this.sqlMapClient;
    }
SqlMapClientFactoryBean 返回的是 getObject() 中返回的 sqlMapClient, 而不是 SqlMapClientFactoryBean 自己的實例。

4. 依賴注入(DI)

1) 依賴注入的方式分為構造函數注入和setter方法注入

<bean id="exampleBean" class="examples.ExampleBean">
    <constructor-arg index="0" value="7500000"/>
    <constructor-arg index="1" ref="bar"/>
</bean>
<bean id="bar" class="x.y.Bar"/>

構造函數注入使用:<constructor-arg index="0" value="7500000"/>, <constructor-arg type="int" value="7500000"/>,對於非簡單參數,需要使用ref <constructor-arg index="1" ref="bar"/>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="configLocation" value="classpath:config/mybatis-config.xml" />
      <property name="mapperLocations" value="classpath*:config/mappers/**/*.xml" />
    </bean>

setter方法注入使用 <property name="username" value="xxx"/>, 非簡單類型屬性使用ref <property name="xxbean" ref="xxx"/>

2)集合等復雜類型的注入:

<bean id="moreComplexObject" class="example.ComplexObject">
    <!-- results in a setAdminEmails(java.util.Properties) call -->
    <property name="adminEmails">
        <props>
            <prop key="administrator">administrator@example.org</prop>
            <prop key="support">support@example.org</prop>
            <prop key="development">development@example.org</prop>
        </props>
    </property>
    <!-- results in a setSomeList(java.util.List) call -->
    <property name="someList">
        <list>
            <value>a list element followed by a reference</value>
            <ref bean="myDataSource" />
        </list>
    </property>
    <!-- results in a setSomeMap(java.util.Map) call -->
    <property name="someMap">
        <map>
            <entry key="an entry" value="just some string"/>
            <entry key ="a ref" value-ref="myDataSource"/>
        </map>
    </property>
    <!-- results in a setSomeSet(java.util.Set) call -->
    <property name="someSet">
        <set>
            <value>just some string</value>
            <ref bean="myDataSource" />
        </set>
    </property>
</bean>

也很簡單,list屬性就是 <list>里面包含<value>或者<ref>或者<bean>, set也類似。map是<map>里面包含<entry>這個也好理解,因為map的實現就是使用內部類Entry來存儲key和value. Properties是 <props>里面包含<prop>.

5. <bean> 元素可以配置的屬性:

<bean> 除了 id 和 class 屬性之外,還有一些可選的屬性:

1) scope屬性,默認<bean> 的 scope就是 singleton="true", springmvc和struts2的重要區別之一就是spring的controll是單例的,而struts2的action是:scope="prototype" ,還有 scope="request" , scope="session",scope="globalSession"(僅用於portlet)

2)abstract屬性,是否是抽象的bean:

    <bean id="baseDAO" abstract="true">
        <property name="dataSource" ref="dataSource" />
        <property name="sqlMapClient" ref="sqlMapClient" />
    </bean>    
    <bean id="collectionDAO" class="net.minisns.dal.dao.CollectionDAOImpl" parent="baseDAO" />
    <bean id="commentDAO" class="net.minisns.dal.dao.CommentDAOImpl" parent="baseDAO" />

3)depends-on 依賴於某個bean,其必須先初始化:<bean id="xxx" class="xxx" depends-on="refbean" />

4)lazy-init="true" 是否延遲初始化,默認為 false

5) dependency-check 是否對bean依賴的其它bean進行檢查,默認值為 none,可取值有:none, simple, object, all等

6)factory-method 和 factory-bean用於靜態工廠和非靜態工廠:

<bean id="bar" class="...StaticBarInterfaceFactory" factory-method="getInstance"/>
<bean id="barFactory" class="...NonStaticBarInterfaceFactory"/> 
<bean id="bar" factory-bean="barFactory" factory-method="getInstance"/>

7)init-method, destory-method 指定bean初始化和死亡時調用的方法,常用於 dataSource的連接池的配置

8) lookup-method 方法注入:

<bean id="newsBean" class="..xxx" singleton="false"> 
<bean id="mockPersister" class="..impl.MockNewsPersister"> <lookup-method name="getNewsBean" bean="newsBean"/> </bean>

表示 mockPersister 有一個依賴屬性 newsBean,該屬性的每次注入都是通過調用newsBean.getNewsBean() 方法獲得的。

9) autowire 是否啟用自動裝配依賴,默認為 no, 其它取值還有:byName, byType, constructor


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM