zoukankan      html  css  js  c++  java
  • spring容器、spring MVC容器以及web容器的区别

    本文转载自 https://www.cnblogs.com/xiexin2015/p/9023239.html

    说到spring和springmvc,其实有很多工作好多年的人也分不清他们有什么区别,如果你问他项目里用的什么MVC技术,他会说我们用的spring和mybatis,或者spring和hibernate。

    在潜意识里会认为springmvc就是spring,之前我也是这么认为的,哈哈。 

      虽然springMVC和spring有必然的联系,但是他们的区别也是有的。下面我就简单描述下

      首先 springmvc和spring它俩都是容器,容器就是管理对象的地方,例如Tomcat,就是管理servlet对象的,而springMVC容器和spring容器,就是管理bean对象的地方,再说的直白点,springmvc就是管理controller对象的容器,spring就是管理service和dao的容器,这下你明白了吧。所以我们在springmvc的配置文件里配置的扫描路径就是controller的路径,而spring的配置文件里自然配的就是service和dao的路径

      spring-mvc.xml

    <context:component-scan base-package="com.smart.controller" />

      applicationContext-service.xml

    <!-- 扫描包加载Service实现类 -->
        <context:component-scan base-package="com.smart.service"></context:component-scan>
    或者
    <context:component-scan base-package="com.smart">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

      至于他是怎么管理起来的,又是怎么注入属性的,这就涉及到他们底层的实现技术了
      其次, spring容器和springmvc容器的关系是父子容器的关系。spring容器是父容器,springmvc是子容器。在子容器里可以访问父容器里的对象,但是在父容器里不可以访问子容器的对象,说的通俗点就是,在controller里可以访问service对象,但是在service里不可以访问controller对象

      所以这么看的话,所有的bean,都是被spring或者springmvc容器管理的,他们可以直接注入。然后springMVC的拦截器也是springmvc容器管理的,所以在springmvc的拦截器里,可以直接注入bean对象。

    <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/employee/**" ></mvc:mapping>
                <bean class="com.smart.core.shiro.LoginInterceptor" ></bean>
            </mvc:interceptor>
        </mvc:interceptors>


      
    而web容器又是什么鬼,
      web容器是管理servlet,以及监听器(Listener)和过滤器(Filter)的。这些都是在web容器的掌控范围里。但他们不在spring和springmvc的掌控范围里。因此,我们无法在这些类中直接使用Spring注解的方式来注入我们需要的对象,是无效的,
    web容器是无法识别的。

      但我们有时候又确实会有这样的需求,比如在容器启动的时候,做一些验证或者初始化操作,这时可能会在监听器里用到bean对象;又或者需要定义一个过滤器做一些拦截操作,也可能会用到bean对象。
    那么在这些地方怎么获取spring的bean对象呢?下面我提供两个方法:

    1、

    public void contextInitialized(ServletContextEvent sce) {
      ApplicationContext context = (ApplicationContext) sce.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 
      UserService userService = (UserService) context.getBean("userService");
    }

    2、

    public void contextInitialized(ServletContextEvent sce) {
      WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()); 
      UserService userService = (UserService) webApplicationContext.getBean("userService"); 
    }

      注意:以上代码有一个前提,那就是servlet容器在实例化ConfigListener并调用其方法之前,要确保spring容器已经初始化完毕!而spring容器的初始化也是由Listener(ContextLoaderListener)完成,因此只需在web.xml中先配置初始化spring容器的Listener,然后在配置自己的Listener。

  • 相关阅读:
    第十章第二节 阿基米德原理
    python-文件读写操作
    pyqt5-控件的显示与隐藏
    pyqt5-控件是否可用
    使用Visual Studio 2015开发Android 程序
    字节序(byte order)和位序(bit order)
    庖丁解牛-----Live555源码彻底解密(根据MediaServer讲解Rtsp的建立过程)
    H264系列(9):H264中的时间戳(DTS和PTS)
    EMIPLIB简介
    minidump-DMP文件的生成和使用
  • 原文地址:https://www.cnblogs.com/by-my-blog/p/11525674.html
Copyright © 2011-2022 走看看