SpringMVC-Spring容器启动加载Mybitas过程
- 本案例用的是tomcat做的servlet容器
- 浅谈一下spring和springMVC的关系,spring项目下有很多模块,mvc是其中之一,各个子模块与父模块相同,父模块不能链接子模块
- 在mvc项目初始化的时候会初始化两个Context,一个是Root WebApplication(mvc的context),一个是ApplicationContext(spring容器)
- spring容器负责ioc aop ,mvc容器负责请求的转发视图解析,mvc容器可以请求spring容器,反之不行
- 当servlet发布ServletContextEvent事件的时候会触发配置在web.xml里的监听
Dao层实际上是一个代理对象,在执行的时候先走MapperProxy.invoke()
spring监听器
org.springframework.web.context.ContextLoaderListener
`
public void refresh() throws BeansException, IllegalStateException {
synchronized(this.startupShutdownMonitor) {
//读取contextConfigLocation值
this.prepareRefresh();
//创建beanFactory
ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
//将spring.xml和mybitas.xml的扫描路径下的类和bean加载到beanFactory
this.prepareBeanFactory(beanFactory);
try {
this.postProcessBeanFactory(beanFactory);
this.invokeBeanFactoryPostProcessors(beanFactory);
this.registerBeanPostProcessors(beanFactory);
this.initMessageSource();
this.initApplicationEventMulticaster();
this.onRefresh();
this.registerListeners();
//发布加载将bean里面的name值设置到对象对应的属性里面
this.finishBeanFactoryInitialization(beanFactory);
this.finishRefresh();
} catch (BeansException var9) {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
}
this.destroyBeans();
this.cancelRefresh(var9);
throw var9;
} finally {
this.resetCommonCaches();
}
}
}
`
- 初始化MapperScannerConfigurer对象,并将datasource和dao路径设置进去
设置 - 2里面的主要方法就是执行了this.buildSqlSessionFactory();
- 这个方法执行了mybitas 配置文件初始化的工作
- 配置文件的加载的精华就在configuration里面,如下
- reflectorFactory存的就是xml里面的entity引用
- interceptorChain存的拦截器,用法详见
- mappedStatements这个是重头,将dao.method做key,自个写的sql做value存在这
- resultMaps看名字就知道是所有的resultMap
总之mapper.xml解析的所有东西都在这WATING
还没有评论,来说两句吧...