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 Configurabl