zoukankan      html  css  js  c++  java
  • SpringMVC 与Spring 整合细节

    SpringMVC依赖于Spring,但是利用SpringIOC配置其他bean的时候回被初始化两次

    web.xml同时配置SpringMVC Servlet过滤和Spring的过滤器

    <!-- spring配置 -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:bean.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <!-- SpringMVC配置 -->
        <servlet>
            <servlet-name>dispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        
        <servlet-mapping>
            <servlet-name>dispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

    这样实体会被初始化两次,解决办法:SpringMVC配置与Spring配置过滤掉扫描的注解

    SpringMVC配置

    <context:component-scan base-package="cn.liangqinghai" use-default-filters="false">
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
            <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
        </context:component-scan>

    其中use-default-filters="false"尤为重要,忽略掉缺省的过滤器

    Spring配置

    <context:component-scan base-package="cn.liangqinghai">
              <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
              <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
          </context:component-scan>
  • 相关阅读:
    (转)AWK函数
    (转)Linux ldconfig 与 ldd指令
    Linux内存管理Swap和Buffer Cache机制
    聚集索引和非聚集索引的区别有哪些
    mod_fastcgi和mod_fcgid的区别
    PHP运行模式
    详解php的安装模式---CGI,FASTCGI,php-fpm,mod_php,mod_cgi,mod_fcgid
    概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM 各公共网关接口介绍
    PHP 5.3以上版本推荐使用mysqlnd驱动
    apache 静态编译和动态编译参考
  • 原文地址:https://www.cnblogs.com/liangqinghai/p/7003410.html
Copyright © 2011-2022 走看看