• spring和springMVC整合父子容器问题:整合Spring时Service层为什么不做全局包扫描详解


    整合Spring时Service层为什么不做全局包扫描详解

    一、Spring和SpringMVC的父子容器关系


    1.讲问题之前要先明白一个关系


    一般来说,我们在整合Spring和SpringMVC这两个框架中,web.xml会这样写到:

     

    复制代码
      <!-- 加载spring容器 -->
      <!-- 初始化加载application.xml的各种配置文件 -->
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/application-*.xml</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    
      <!-- 配置springmvc前端控制器 -->
      <servlet>
        <servlet-name>taotao-manager</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation,
         springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
    复制代码

     

    首先配置的是Spring容器的初始化加载的application文件,然后是SpringMVC的前端控制器(DispatchServlet),当配置完DispatchServlet后会在Spring容器中创建一个新的容器。其实这是两个容器,Spring作为父容器,SpringMVC作为子容器。

     

     

     

     

    平时我们在项目中注入关系是这样的顺序(结合图来说):在Service中注入Dao(初始化自动注入,利用@Autowired),接着在Controller里注入Service(初始化自动注入,利用@Autowired),看图,这就意味这作为SpringMVC的子容器是可以访问父容器Spring对象的。

    么问大家一个问题。要是反过来呢,你把Controller注入到Service中能行么?
    肯定是不行的啊!(如图,这也说明了父容器是不能调用子容器对象的)

     

     

    如果Dao,Serive,Controller要是都在Spring容器中,无疑上边的问题是肯定的,因为都是在一个bean里,一个容器中。

     


    2.问题:为什么不能在Spring中的Service层配置全局扫描?

     

    例如:一个项目中我总项目的名字叫com.shop,我们在配置applicationContext-service.xml中,包扫描代码如下:

    复制代码
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" 
        ...../ 此处省略>
    
        <!-- 扫描包Service实现类 -->
        <context:component-scan base-package="com.shop.service"></context:component-scan>
    </beans>
    复制代码

    上面所配置的是一个局部扫描,而不是全局扫描。接下来说原因:

    这里就和上面讲到的父子容器有关系,假设我们做了全局扫描那么代码如下:

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    ...../ 此处省略>
     
      
     
    <!-- 扫描包Service实现类 -->
    <context:component-scan base-package="com.shop"></context:component-scan>
    </beans>

      

     


    此时的Spring容器中就会扫描到@Controller,@Service,@Reposity,@Component,此时的图如下

     

     

    结合图去看,相当于他们都会放到大的容器中,而这时的SpringMVC容器中没有对象,没有对象就没有Controller,所以加载处理器,适配器的时候就会找不到映射对象,映射关系,因此在页面上就会出现404的错误。


    3.如果不用Spring容器,直接把所有层放入SpringMVC容器中可不可以?


      当然可以,如果没有Spring容器,我们是可以把所有层放入SpringMVC的。单独使用这个容器是完全可以的,而且是轻量级的。


    4.那么为什么我们在项目中还要联合用到Spring容器和SpringMVC容器?

      答案是: Spring的扩展性,如果要是项目需要加入Struts等可以整合进来,便于扩展框架。如果要是为了快,为了方便开发,完全可以用SpringMVC框架。


    5.结论

      如果在项目中我们在Service层做全局包扫描,那么springmvc不能提供服务,因为springmvc子容器中没有controller对象。

     

  • 相关阅读:
    考研英语各题型的正确率情况
    青少年python系列 25.turtle库绘制一个田字方格的方框
    FreeSWITCH使用L16编码通信及raw数据提取
    【Qt炫酷动画】demo02-仿苹果对话框淡入淡出的动画
    【有趣的Python小程序】Python让我们的手指在停课浪潮中变得更加灵活 被老师点到再也不害怕 / Pygame制作键盘打字游戏KeyBoardFlash
    nginx能干什么?
    瀚高数据库开机自启动失败
    软件测试【秋招面试】字节跳动等各类大厂—面经
    微服务和Spring Cloud Alibaba介绍
    Useful Policy Invariant Shaping from Arbitrary Advice论文翻译导读分析
  • 原文地址:https://www.cnblogs.com/bakabill/p/16204324.html