SpringMVC-與Spring的整合

一.概述

1.1 提出問題

  1. SpringMVC是Spring中的一部分,那麼需要進行Spring整合SpringMVC?
  2. 還是否需要在加入Spring的IOC容器?
  3. 是否需要在web.xml文件中配置啓動Spring IOC容器的ContextLoadListener?

1.2 解決問題

  1. 需要。SpringMVC的配置文件就來配置和網站轉發邏輯以及網站功能有關(視圖解析器,文件上傳解析器,支持ajax...)。Spring的配置文件和業務有關的(事務控制,數據源...)。
  2. 需要 放入Spring配置文件對應的IOC容器中還有Service和Dao。
  3. 既需要又不需要,不需要:都放在SpringMVC的配置文件,也可以分爲多個Spring的配置文件,然後使用import節點導入其他的配置文件

二.Spring整合SpringMVC實驗解決方法

我們已經知道Spring和SpringMVC分容器:Spring管理業務邏輯組件;SpringMVC管理控制器組件;

1.在web.xml中配置Spring啓動時用的監聽器和SpringMVC的前端控制器:

<?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>6.Spring_crud</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <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>
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->

  <!-- Spring監聽器配置 -->
  <!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring.xml</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

  <!-- SpringMVC前端控制器配置 -->
	<servlet>
		<servlet-name>springDispatcherServlet</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>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	<filter>
	<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
	<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 支持Rest風格轉換的filter -->
	<filter>
	<filter-name>HiddenHttpMethodFilter</filter-name>
	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
	</filter>
	<filter-mapping>
	<filter-name>HiddenHttpMethodFilter</filter-name>
	<url-pattern>/*</url-pattern>
	</filter-mapping>
	
</web-app>

2.創建Spring的配置文件:spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.test">
   <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!--配置數據源,整合其他框架,事務等-->
</beans>

3.創建SpringMVC的配置文件:springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.test" use-default-filters="false">

    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

  <!-- 配置視圖解析器 -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResouceViewResolver">
     <property name="suffix" value="/WEB-INF/pages/"></property>
     <property name="prefix" value=".jsp"></property>
  </bean>

<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>

</beans>

問題:若只單單設定包掃描組件,若Spring的IOC容器和SpringMVC的IOC容器掃描的包有重合的部分,就會導致有bean被創建2次。解決方法:使用exclude-filter和include-filter子節點來規定掃描的註解。

4.測試:創建BookController和BookService,在這兩個類中增加構造方法,啓動服務器,查看構造器執行情況。

package com.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.test.service.BookService;

@Controller
public class BookController {
	
	@Autowired
	private BookService bookService;
	
	public BookController()
	{
		System.out.println("BookController...");
	}
	
	
	@RequestMapping("/hello")
	public String hello(){
		System.out.println(bookService);
		return "forward:/index.jsp";
	}

}


package com.test.service;

import org.springframework.stereotype.Service;

@Service
public class BookService {

	public BookService(){
		System.out.println("BookService...");
		
		
	}
}

三.Spring IOC容器和SpringMVC IOC容器的關係

在上面整合中,Controller和Service兩個組件分別在不同的容器中,但是可以在Controller中自動注入Service。這就說明:SpringMVC的IOC容器中的bean可以來引用Spring容器中的bean。反過來呢?反之則不行。Spring IOC容器中的bean卻不能引用Spring MVC IOC容器中的bean。

  • 在SpringMVC配置文件中引用業務層的bean。
  • 多個Spring IOC容器中間可以設置爲父子關係,以實現良好的解耦
  • Spring MVC WEB容器可作爲“業務層”Spring 容器的子容器:即WEB層容器可以引用業務層容器的Bean,而業務層容器卻訪問不到WEB容器的Bean。

四.SpringMVC對比Struts

①. Spring MVC 的入口是 Servlet, 而 Struts2 是 Filter

②. Spring MVC 會稍微比 Struts2 快些. Spring MVC 是基於方法設計, 而 Sturts2 是基於類, 每次發一次請求都會實例一個 Action.

③. Spring MVC 使用更加簡潔, 開發效率Spring MVC確實比 struts2 高: 支持 JSR303, 處理 ajax 的請求更方便

④. Struts2 的 OGNL 表達式使頁面的開發效率相比 Spring MVC 更高些

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章