Spring 3.0之后集成Spring框架
web.xml
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
ContextLoaderListener实现ServletContextListener接口,在contextInitialized方法中完成Spring的加载
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}
其中主要逻辑都在子类contextInitialized中initWebApplicationContext方法中实现:
- 创建WebApplicationContext:createWebApplicationContext(servletContext);
<!-- 通过servletContext.getInitParameter("contextConfigLocation")获取--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 通过servletConfig.getInitParameter("contextConfigLocation")获取--> <servlet> <servlet-name>TestServlet</servlet-name> <servlet-class>com.xxx.xxx.TestServlet</servlet-class> <init-param> <param-name>initParam</param-name> <param-value>avalible in servlet init()</param-value> </init-param> </servlet>
- 优先通过servletContext.getInitParameter取web.xml里key为 contextClass配置的类(必须为ConfigurableWebApplicationContext的子类)实例化
- 其次使用(ContextLoader.properties中配置)默认类
org.springframework.web.context.support.XmlWebApplicationContext
- 配置刷新applictionContext的值configureAndRefreshWebApplicationContext
- StandardEnvironment.initPropertySources
- customizeContext(sc, wac)
初始化配置在web.xml中的参数globalInitializerClasses、contextInitializerClasses中,并且类型为ApplicationContextInitializer的类,执行initialize方法🚩 - wac.fresh() spring加载核心
fresh方法
1. prepareRefresh
- StandardEnvironment.initPropertySources
- StandardEnvironment.validateRequiredProperties 未找到数据入口?
2. obtainFreshBeanFactory 建立BeanFactory,只有bean定义
- 销毁已经存在的beanFactory
- createBeanFactory parent存在就继承parent配置,BeanNameAware、BeanFactoryAware、BeanClassLoaderAware加入到依赖检查忽略列表ignoredDependencyInterfaces,主要功能是忽略给定接口的自动装配功能,因为类的初始化需要前置检查依赖注入是否都注入成功🙈
- customizeBeanFactory 自定义是否运行bean定义覆盖和单例bean的循环引用
- loadBeanDefinitions 加载bean定义,根据具体实现类分为不同的方式,不如xml配置、注解等(后续详细解析)
3. prepareBeanFactory BeanFactory创建的前期准备
- 1 ApplicationContextAwareProcessor BeanPostProcessor处理ApplicationContext相关的aware接口注入
- 2 ApplicationContext相关aware接口加入到依赖检查忽略列表ignoredDependencyInterfaces
- 3 BeanFactory、ResourceLoader、ApplicationEventPublisher、ApplicationContext等自动注入类型候选集
/** Map from dependency type to corresponding autowired value */
private final Map<Class<?>, Object> resolvableDependencies = new ConcurrentHashMap<>(16);
- 4 ApplicationListenerDetector BeanPostProcessor处理ApllicationListener注册
/** Helper class used in event publishing */
private ApplicationEventMulticaster applicationEventMulticaster;
/** Statically specified listeners */
private final Set<ApplicationListener<?>> applicationListeners = new LinkedHashSet<>();
- 5 LoadTimeWeaverAwareProcessor BeanPostProcessor处理LoadTimeWeaverAware注入
LoadTimeWeaver 待延伸,静态动态织入
<!-- 注解方式 -->
@Configuration
@EnableLoadTimeWeaving
public class AppConfig {}
<!-- xml配置方式-->
<beans>
<context:load-time-weaver/>
</beans>
- 6 加载默认environment beans
environment、systemProperties(System#getProperties)、systemEnvironment(System#getenv)
4. postProcessBeanFactory
- 1 ServletContextAwareProcessor BeanPostFactory处理ServletContextAware、ServletConfigAware注入
- 2 ServletContextAware、ServletConfigAware接口加入到依赖检查忽略列表ignoredDependencyInterfaces
- 3 注册request(RequestScope)、session(SessionScope)、application(ServletContextScope)等scope
- 4 ServletRequest、ServletResponse、HttpSession、WebRequest等自动注入类型候选集
- 5 注册单例bean:servletContext、servletConfig、contextParameters、contextAttributes
5. invokeBeanFactoryPostProcessors
- 执行所有的 BeanDefinitionRegistryPostProcessor#postProcessBeanDefinitionRegistry🚩按PriorityOrdered->Ordered->其他
- 执行所有的 BeanFactoryPostProcessor#postProcessBeanFactory🚩按PriorityOrdered->Ordered->其他
- 清除mergedBeanDefinitions中未包含在alreadyCreated中的
- 清除allBeanNamesByType,singletonBeanNamesByType数据,用于按类型注入
- 执行3.5
6. registerBeanPostProcessors
- 注册并实例化BeanPostProcessor🚩按PriorityOrdered->Ordered->其他 (getBean 后续详细阐述🙈)
- 注册MergedBeanDefinitionPostProcessor
- 执行3.4
7. initMessageSource 初始化MessageSource,用于国际化,beanName:messageSource
8. initApplicationEventMulticaster 初始化event multicaster
9. onRefresh🚩模板方法,提供自定义加载扩展点
10. registerListeners
- 将所有接口ApplicationListener注册到applicationEventMulticaster,并发布earlyApplicationEvents
11. finishBeanFactoryInitialization
- 设置beanFactory的conversionService
- 如果没有embeddedValueResolvers,默认使用getEnvironment()#resolvePlaceholders
- 初始化所有LoadTimeWeaverAware类(getBean)
- freezeConfiguration 冻结配置,getBeanNamesForType完全通过缓存读取
- preInstantiateSingletons 实例化所有的非lazyInit的单例bean(getBean)
- SmartFactoryBean执行isEagerInit🚩
- SmartInitializingSingleton执行afterSingletonsInstantiated
12.finishRefresh
- clearResourceCaches 清楚resource缓存
- 初始化LifecycleProcessor,默认DefaultLifecycleProcessor,并执行onRefresh
- 发布ContextRefreshedEvent
- LiveBeansView.registerApplicationContext JMX相关
网友评论