SpringMVC-Spring容器启动加载Mybitas过程

柔光的暖阳◎ 2023-06-27 05:55 141阅读 0赞
  • 本案例用的是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

在这里插入图片描述`

  1. public void refresh() throws BeansException, IllegalStateException {
  2. synchronized(this.startupShutdownMonitor) {
  3. //读取contextConfigLocation值
  4. this.prepareRefresh();
  5. //创建beanFactory
  6. ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
  7. //将spring.xml和mybitas.xml的扫描路径下的类和bean加载到beanFactory
  8. this.prepareBeanFactory(beanFactory);
  9. try {
  10. this.postProcessBeanFactory(beanFactory);
  11. this.invokeBeanFactoryPostProcessors(beanFactory);
  12. this.registerBeanPostProcessors(beanFactory);
  13. this.initMessageSource();
  14. this.initApplicationEventMulticaster();
  15. this.onRefresh();
  16. this.registerListeners();
  17. //发布加载将bean里面的name值设置到对象对应的属性里面
  18. this.finishBeanFactoryInitialization(beanFactory);
  19. this.finishRefresh();
  20. } catch (BeansException var9) {
  21. if (this.logger.isWarnEnabled()) {
  22. this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
  23. }
  24. this.destroyBeans();
  25. this.cancelRefresh(var9);
  26. throw var9;
  27. } finally {
  28. this.resetCommonCaches();
  29. }
  30. }
  31. }
  32. `
  • 初始化MapperScannerConfigurer对象,并将datasource和dao路径设置进去
    在这里插入图片描述
    设置在这里插入图片描述
  • 2里面的主要方法就是执行了this.buildSqlSessionFactory();
  • 这个方法执行了mybitas 配置文件初始化的工作
  • 配置文件的加载的精华就在configuration里面,如下
  • reflectorFactory存的就是xml里面的entity引用
  • interceptorChain存的拦截器,用法详见
  • mappedStatements这个是重头,将dao.method做key,自个写的sql做value存在这
  • resultMaps看名字就知道是所有的resultMap
    总之mapper.xml解析的所有东西都在这
    在这里插入图片描述WATING

发表评论

表情:
评论列表 (有 0 条评论,141人围观)

还没有评论,来说两句吧...

相关阅读

    相关 tomcat容器部分过程

    看到一篇关于web.xm文件中标签的讲解,顺带还阐述了容器的工作流程,因此转载此,以供参考,原文地址: [与的区别与作用][Link 1] <context-par