Spring的ApplicationContext实例化源码解析

来源:互联网 发布:python数据挖掘介绍 编辑:程序博客网 时间:2024/06/07 23:00

Spring的ApplicationContext来获得Bean的操作,通过ApplicationContext context = new ClassPathXmlApplicationContext("spring/test-bean.xml");这个代码进行了调试,于是把整个流程做了整理

在看下面的流程前可以看看下面的类图:


整个流程中,Spring留了很多可以覆盖的方法和需要实现的接口,可以方便我们在整个流程中的适当地方个性化自己的功能。

1.AbstractApplicationContext.java中的protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException

/**
* Subclasses must implement this method to perform the actual configuration load.
* The method is invoked by {@link #refresh()} before any other initialization work.
* <p>A subclass will either create a new bean factory and hold a reference to it,
* or return a single BeanFactory instance that it holds. In the latter case, it will
* usually throw an IllegalStateException if refreshing the context more than once.
* @throws BeansException if initialization of the bean factory failed
* @throws IllegalStateException if already initialized and multiple refresh
* attempts are not supported
*/

       继承AbstractApplicationContext,实现这个方法,在这个方法中构造出自己特有的BeanFactory,可以参照AbstractRefreshableApplicationContext.java对于这个的实现 

       refreshBeanFactory会调用在AbstractRefreshableApplicationContext.java中的loadBeanDefinitions方法,这个方法是抽象的,可以交给具体的实现,比如支持从XML文件获取Bean的配置信息,也可以实现这个方法,在这个方法中从数据库,从远程服务上获取Bean

2.AbstractApplicationContext.java中的public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException

/**
* Subclasses must return their internal bean factory here. They should implement the
* lookup efficiently, so that it can be called repeatedly without a performance penalty.
* <p>Note: Subclasses should check whether the context is still active before
* returning the internal bean factory. The internal factory should generally be
* considered unavailable once the context has been closed.
* @return this application context's internal bean factory (never <code>null</code>)
* @throws IllegalStateException if the context does not hold an internal bean factory yet
* (usually if {@link #refresh()} has never been called) or if the context has been
* closed already
* @see #refreshBeanFactory()
* @see #closeBeanFactory()
*/

        继承AbstractApplicationContext,实现这个方法,在这个方法中返回上面refreshBeanFactory中构造出来的BeanFactory,可以参照AbstractRefreshableApplicationContext.java对于这个的实现


3.AbstractApplicationContext.java中的protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)

/**
* 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 registering special
* BeanPostProcessors etc in certain ApplicationContext implementations.
* @param beanFactory the bean factory used by the application context
*/

        继承AbstractApplicationContext,实现这个方法,在这个方法中可以添加自己的BeanPostProcessors,当然通过Spring文件配置也是可以的,因为后面registerBeanPostProcessors会从Spring配置文件中查找实现BeanPostProcessor的类,当然也可以注册自己的BeanFactoryPostProcessors


4.BeanFactoryPostProcessor.java中的void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException

/**
* 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
*/

         这里可以做和上面3同样的事情


5.BeanPostProcessor.java中的Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException

/**
* Apply this BeanPostProcessor to the given new bean instance <i>before</i> any bean
* initialization callbacks (like InitializingBean's <code>afterPropertiesSet</code>
* 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.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
*/

       这个方法会在类的实例化之前执行,所以说执行顺序是postProcessBeforeInitialization->afterPropertiesSet->init-method

==================================上面5个方法都是Bean还没有实例化之前执行的=============================================

6.BeanPostProcessor.java中的Object postProcessAfterInitialization(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</code>
* 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.
* <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
* instance and the objects created by the FactoryBean (as of Spring 2.0). The
* post-processor can decide whether to apply to either the FactoryBean or created
* objects or both through corresponding <code>bean instanceof FactoryBean</code> checks.
* <p>This callback will also be invoked after a short-circuiting triggered by a
* {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
* in contrast to all other BeanPostProcessor callbacks.
* @param bean the new bean instance
* @param beanName the name of the bean
* @return the bean instance to use, either the original or a wrapped one
* @throws org.springframework.beans.BeansException in case of errors
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
* @see org.springframework.beans.factory.FactoryBean
*/

       这个方法会在类的实例化之后执行,所以说执行顺序是postProcessBeforeInitialization->afterPropertiesSet->init-method->postProcessAfterInitialization


下面是详细的时序图:


原创粉丝点击