框架整合——Spring與SpringMVC框架整合步驟與優勢講解

来源:http://www.cnblogs.com/Mr-zhaoz/archive/2017/09/03/7471000.html
-Advertisement-
Play Games

Spring與SpringMVC整合! 問:實際上SpringMVC就運行在Spring環境之下,還有必要整合麽?SpringMVC和Spring都有IOC容器,是不是都需要保留呢? 答案是:通常情況下,類似於數據源,事務,整合其他框架都是放在spring的配置文件中(而不是放在SpringMVC的 ...


Spring與SpringMVC整合!

  問:實際上SpringMVC就運行在Spring環境之下,還有必要整合麽?SpringMVCSpring都有IOC容器,是不是都需要保留呢?

  答案是:通常情況下,類似於數據源,事務,整合其他框架都是放在spring的配置文件中(而不是放在SpringMVC的配置文件中),實際上放入Spring配置文件對應的IOC容器中的還有ServiceDao.SpringMVC也搞自己的一個IOC容器,在SpringMVC的容器中只配置自己的Handler(Controller)信息。所以,兩者的整合是十分有必要的,SpringMVC負責接受頁面發送來的請求,Spring框架則負責整理中間需求邏輯,對資料庫發送操作請求,對資料庫的操作目前則先使用Spring框架中的JdbcTemplate進行處理。

1.導入SpringSpringMVC的所有jar

c3p0-0.9.1.2.jar

com.springsource.net.sf.cglib-2.2.0.jar

com.springsource.org.aopalliance-1.0.0.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

commons-logging-1.1.3.jar

mysql-connector-java-5.1.37-bin.jar

spring-aop-4.0.0.RELEASE.jar

spring-aspects-4.0.0.RELEASE.jar

spring-beans-4.0.0.RELEASE.jar

spring-context-4.0.0.RELEASE.jar

spring-core-4.0.0.RELEASE.jar

spring-expression-4.0.0.RELEASE.jar

spring-jdbc-4.0.0.RELEASE.jar

spring-orm-4.0.0.RELEASE.jar

spring-tx-4.0.0.RELEASE.jar

spring-web-4.0.0.RELEASE.jar

spring-webmvc-4.0.0.RELEASE.jar

 

2.web.xml文件中分別配置SpringMVCSpring的配置信息

  

   <!--  ContextLoaderListener 載入IOC容器,Spring框架的底層是listener -->
     <context-param>
		<param-name>contextConfigLocation</param-name>
		<!-- 指定Spring的配置文件的路徑和名稱 -->
		<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 配置文件 SpringMVC底層是servlet  -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
              <!-- 指定SpringMVC框架的配置文件的路徑和名稱 -->
              <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>


	<!-- 設置編碼,處理post請求亂碼 -->
	<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>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<!-- post請求轉化為put和delete -->
	<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>

  

3.配置spring的配置文件和springmvc的配置文件

 spring的配置文件:

 

<!-- 配置掃描的包 -->

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

<!-- 載入properties文件中 信息 -->

<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 配置數據源 -->

<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

  <property name="user" value="${jdbc.user}"></property>

  <property name="password" value="${jdbc.passowrd}"></property>

  <property name="jdbcUrl" value="${jdbc.url}"></property>

  <property name="driverClass" value="${jdbc.driver}"></property>

</bean>

 

<!-- 配置JdbcTemplate對應的bean, 並裝配dataSource數據源屬性-->

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">

  <property name="dataSource" ref="comboPooledDataSource"></property>

</bean>

<!-- 為了執行帶有具名參數的SQL語句,需要配置NamedParameterJdbcTemplate -->

<!-- 該NamedParameterJdbcTemplate類沒有無參構造器,需要傳入JdbcTemplate對象或者數據源對象[DataSource] -->

<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">

  <!-- 不能使用property標簽配置哦 -->

  <constructor-arg ref="jdbcTemplate"></constructor-arg>

</bean>

 

  

 

springmvc的配置文件:   

<!-- 配置掃描的包 -->
<context:component-scan base-package="com.neuedu"></context:component-scan>

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

<!-- 可以處理靜態資源 -->
<mvc:default-servlet-handler/>

<!-- 可以從一個頁面直接通過請求到達另一個頁面,不用通過controller -->
<!-- <mvc:view-controller path="/" view-name="/" /> -->
<!-- 若只有mvc:default沒有mvc:annotation-driven,正常的requestMapping不能被訪問,所以它倆是標配 -->

<mvc:annotation-driven></mvc:annotation-driven>

  

 加入jdbc.properties文件:

jdbc.user=root

jdbc.passowrd=123456
jdbc.url=jdbc:mysql://localhost:3306/jdbc_template
jdbc.driver=com.mysql.jdbc.Driver

  

4.創建Controller類與Service類,並創建這兩個類的無參構造器,分別輸出一句話!

5.啟動項目,會發現controller構造器和service構造器都執行了兩次!

問題:若SpringIOC容器和SpringMVCIOC容器掃描的包有重合的部分,就會導致有的bean會被創建2次!

解決:

    1.使SpringIOC容器掃描的包和SpringMVCIOC容器掃描的包沒有重合的部分!

    controller層都在controller包,service層都在service

 2.但是有的時候開發的時候是分模塊開發的,這樣不太容易做到,所以:

    可以在component-scan標簽下麵中使用如下子標簽來規定只能掃描的註解:

<context:component-scan base-package="com.neuedu">
  <context:exclude-filter type="annotation" expression=""/>
  <context:include-filter type="annotation" expression=""/>
</context:component-scan>

  

  所以在springMVC的配置文件中我們可以按著如下配置,只掃描controllerControllerAdvice註解:

<context:component-scan base-package="com.neuedu" use-default-filters="false">
  <!-- 掃描註解為@Controller的類 -->
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  <!-- ControllerAdvice註解用來處理全局異常,可以標記在類上,故此處為掃描註解為@ControllerAdvice的類 -->
  <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

  

  而在spring的配置文件中:

<context:component-scan base-package="com.neuedu">
  <!-- 掃描除了註解為@Controller的類 -->
  <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
  <!-- 掃描除了註解為@ControllerAdvice的類 -->
  <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

   此時再重新啟動項目就會發現springspringmvc的對象都創建了一份!

 

6.SpringIOC容器和SpringMVCIOC容器的關係

外邊beans.xml為Spring的配置文件,黃色方格Spring-mvc.xml為SpringMVC的配置文件

註意:

1.SpringMVC容器中的bean可以引用Spring容器中的bean也就是在Controller中我們可以註入service層對象【可以在controller層的requestMapping方法中列印service對象試一下】!

2.反之則不行,就是說:在spring掃描的service層不能引用springmvchandler(Controller)對象【註解一個小例子,啟動項目就會出錯】

3.實際上Spring的容器和Spring容器有父子間關係,【參考圖片】就像兒子可以繼承父親的基因一樣,父親沒法繼承兒子的基因!

  而且從另一個角度也說明瞭Handler(Controller)是可以依賴Service層的,但是Service層卻不可以依賴Handler(Controller)層!


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Response.Write 、RegisterClientScriptBlock和RegisterStartupScript總結 Page.ClientScript.RegisterStartupScript用法小結 原文鏈接:http://blog.csdn.net/qiujialongjjj/ ...
  • 套用mui官方文檔的一句話:“開發者只需關心業務邏輯,實現載入更多數據即可”。真的是不錯的框架。 想更多的瞭解這個框架:http://dev.dcloud.net.cn/mui/ 那麼如何實現下拉刷新,上拉載入的功能呢? 首先需要一個容器: 然後進行初始化操作,通過mui.init方法中pullRe ...
  • 一、WCF技術我該如何學習? 阿笨的回答是:作為初學者的我們,那麼請跟著阿笨一起玩WCF吧,阿笨將帶領大家如何以正確的姿勢去掌握WCF技術。由於WCF技術知識點太多了,就純基礎概念性知識都可以單獨出一本書來講解,本次分享課程《C#面向服務編程技術WCF從入門到實戰演練》開課之前,阿笨還是希望從沒瞭解 ...
  • 開篇先來說一下我和來畫的故事,以及寫這篇文章的初衷。 今年年初時,我還在北京,在 Face++,做著人臉識別技術的 Windows 和 Android 端,做著人工智慧終將實現世間所有美好的夢。這時的我已經離開 UWP,甚至 C# 很久了,寫著 C++ 和 Java,當時真的沒想過會再次回到 UWP ...
  • 自己畫一個轉圈圈的控制項 ...
  • 首先說一下foreach有的也叫增強for迴圈,foreach其實是for迴圈的一個特殊簡化版。 再說一下foreach的書寫格式: for(元素類型 元素名稱 : 遍曆數組(集合)(或者能進行迭代的)){ 語句 } foreach雖然是for迴圈的簡化版本,但是並不是說foreach就比for更好 ...
  • 特別聲明本隨筆copy於egon(林海峰)。 一 IO模型介紹 為了更好地瞭解IO模型,我們需要事先回顧下:同步、非同步、阻塞、非阻塞 同步(synchronous) IO和非同步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分別是什麼,到底有 ...
  • 建立資料庫訪問類的封裝 <?php class DBDA { public $host = "localhost"; //伺服器地址 public $uid = "root"; //資料庫的用戶名 public $pwd = ""; //資料庫的密碼 public $dbname = "";//數據 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...