ApplicationContext 接口关系
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory, MessageSource, ApplicationEventPublisher, ResourcePatternResolver { // 各种方法 }
EnvironmentCapable :提供对环境属性的访问。ListableBeanFactory :允许列举所有 bean 实例,而不是按名字逐个查找。HierarchicalBeanFactory :允许一个 bean 工厂成为另一个 bean 工厂的子工厂。MessageSource :国际化功能。ApplicationEventPublisher :事件发布功能。ResourcePatternResolver :资源加载功能。
实现类 ClassPathXmlApplicationContext
一个非常常见的
public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext { private String[] configLocations; public ClassPathXmlApplicationContext(String configLocation) throws BeansException { this(new String[] {configLocation}, true, null); } public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, @Nullable ApplicationContext parent) throws BeansException { super(parent); setConfigLocations(configLocations); if (refresh) { refresh(); // 初始化 ApplicationContext } } @Override protected String[] getConfigLocations() { return this.configLocations; } // 其他方法... }
refresh 方法解析
public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext { @Override public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { // Prepare this context for refreshing. prepareRefresh(); // Tell the subclass to refresh the internal bean factory. ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); // Prepare the bean factory for use in this context. prepareBeanFactory(beanFactory); try { // Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory); // Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(beanFactory); // Register bean processors that intercept bean creation. registerBeanPostProcessors(beanFactory); // Initialize message source for this context. initMessageSource(); // Initialize event multicaster for this context. initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses. onRefresh(); // Check for listener beans and register them. registerListeners(); // Instantiate all remaining (non-lazy-init) singletons. finishBeanFactoryInitialization(beanFactory); // Last step: publish corresponding event. finishRefresh(); } // 其他异常处理代码... } } // 其他方法... }
- prepareRefresh():准备上下文,设置开始时间、活动标志等。
- obtainFreshBeanFactory():刷新内部的 BeanFactory,并获取它。
- prepareBeanFactory(beanFactory):设置类加载器、后置处理器等。
- postProcessBeanFactory(beanFactory):提供子类覆写的扩展点,用于添加额外的处理。
- invokeBeanFactoryPostProcessors(beanFactory):调用所有注册的 BeanFactoryPostProcessor。
- registerBeanPostProcessors(beanFactory):注册 BeanPostProcessor。
- initMessageSource():初始化消息资源处理,用于国际化。
- initApplicationEventMulticaster():初始化事件广播器。
- onRefresh():由子类覆写,用于特定上下文的 bean 初始化。
- registerListeners():注册监听器。
- finishBeanFactoryInitialization(beanFactory):初始化剩余的单例 beans。
- finishRefresh():最后一步,广播相关事件(比如上下文刷新事件)。
总结
正是通过这个复杂而强大的初始化过程,Spring 能够管理应用中的 beans,并提供如 AOP、事务管理、事件发布等特性。虽然