架構整合——Spring與SpringMVC架構整合步驟與優勢講解,springspringmvc

來源:互聯網
上載者:User

架構整合——Spring與SpringMVC架構整合步驟與優勢講解,springspringmvc
Spring與SpringMVC整合!

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

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

1.匯入Spring和SpringMVC的所有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檔案中分別配置SpringMVC和Spring的配置資訊

  

   <!--  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構造器都執行了兩次!

問題:若Spring的IOC容器和SpringMVC的IOC容器掃描的包有重合的部分,就會導致有的bean會被建立2次!

解決:

    1.使Spring的IOC容器掃描的包和SpringMVC的IOC容器掃描的包沒有重合的部分!

    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的設定檔中我們可以按著如下配置,只掃描controller及ControllerAdvice註解:

<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>

   此時再重新啟動項目就會發現spring和springmvc的對象都建立了一份!

 

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

外邊beans.xml為Spring的設定檔,黃色方格Spring-mvc.xml為SpringMVC的設定檔

注意:

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

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

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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.