Spring + SpringMVC 整合

1. 本身的理解
html

  •     spirngmvc有本身的容器,Spring 也有本身的IOC容器,在整合的時候如何處理呢?java

    在網上查了資料,能夠知道,SpringMVC 主要的做用是攔截客戶端的請求,以後交給Controller, Controller再調用service層; 這樣,咱們在整合的時候就能夠這樣來處理了:web

  • SpringMVC IOC 容器管理controller bean,其餘bean 由Spring管理,這樣就能夠解決了
  • SpringMVC IOC 容器和 Spring IOC 容器有什麼管理呢?spring

  • 在網上查了一些資料後,知道SpringMVC IOC 管理的bean 能夠調用 Spring 管理的bean,反之,Spring中的 bean 不能調用 SrpingMVC中的bean; 總結得出, Spring 容器 是SpringMVC 容器的父類。  具體緣由應該能夠查看源代碼,可是目前實力不夠,因此沒去看。

2. 具體步驟:express

  • 引用的jar包:基本和Spring的架構包不變,主要添加了spring 源碼中沒有的jar包:commons-logging  standardspring-mvc

  • web.xml 文件架構


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringMVCHibernate</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:config/spring-bean.xml</param-value>
  </context-param>
  
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置SpringMVC dispatchServlet -->
  <servlet>
   <servlet-name>springMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:config/spring-mvc.xml</param-value>
      </init-param>
   <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>springMVC</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>
<!-- 配置編碼 -->
  <filter>  
        <filter-name>encodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter> 
    <filter-mapping>  
        <filter-name>encodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>
    
</web-app>
  • Springmvc 配置文件mvc


  • <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"    
     xmlns:context="http://www.springframework.org/schema/context"    
     xmlns:p="http://www.springframework.org/schema/p"  
     xmlns:mvc="http://www.springframework.org/schema/mvc"    
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
     xsi:schemaLocation="http://www.springframework.org/schema/beans    
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
          http://www.springframework.org/schema/context    
          http://www.springframework.org/schema/context/spring-context-3.0.xsd    
          http://www.springframework.org/schema/mvc    
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
     
       <!-- 配置自動掃描的包,自動構建bean -->
       <!-- 
          Spring 和 SpringMVC 整合的時候的兩種方法
          1. 將spring IOC 和 SpringMVC IOC 容器管理的 bean 沒有重合的部分
          2. 使用特定的標籤來區分管理的bean
        -->
       <context:component-scan base-package="com.shangshe.controller" ></context:component-scan>
     <!--   用 filter 掃描特定的註解    :  只掃描 controller 和 controllerAdvice(異常)標籤
       <context:component-scan base-package="com.shangshe" use-default-filters="true">
          <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>
       
        -->
       
       
       <!-- 配置視圖解析器 -->
       <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
          <property name="prefix" value="/" />
          <property name="suffix" value=".jsp" />
       </bean>
       
       <!-- 弄明白這2個標籤是幹什麼用的
       <mvc:default-servlet-handler/>
       <mvc:annotation-driven></mvc:annotation-driven>
        -->
    </beans>
  • spring 配置文件app


  • <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"    
     xmlns:context="http://www.springframework.org/schema/context"    
     xmlns:p="http://www.springframework.org/schema/p"  
     xmlns:mvc="http://www.springframework.org/schema/mvc"    
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
     xsi:schemaLocation="http://www.springframework.org/schema/beans    
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
          http://www.springframework.org/schema/context    
          http://www.springframework.org/schema/context/spring-context-3.0.xsd    
          http://www.springframework.org/schema/mvc    
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
          
          <!-- 自動構建 bean -->
          <context:component-scan base-package="com.shangshe.service" />
          
          <!-- 整合其餘框架  -->
          
          
    </beans>
相關文章
相關標籤/搜索