使用maven建立簡單的多模組 Spring Web專案
第一次寫技術文章,主要內容是使用maven建立一個簡單的SpringMVC WEB 專案,如有操作或理解錯誤請務必指出,當謙虛學習。
做這一次的工作主要是因為想加強一下自己對Spring Web 專案的理解,因為平時都是直接寫業務程式碼,我覺得還是有必要自己瞭解一下建立專案的過程。後續會基於這個專案寫更多的SpringWeb開發過程,希望能幫助到有需要的人。
總的來說是一個相當精簡的多模組springWeb專案搭建過程,讓我們進入正題吧
我們知道單體應用,就寫在一個project裡面的話,業務一旦龐大起來非常難以管理。 把各個模組單獨抽出來可以方便的對jar包進行版本管理(儘管我還沒經歷過這個),維護專案,團隊開發也會方便許多。 基本思想其實就是一個java web專案引用別的模組Jar包,最終web專案被打成war包釋出。 **而所有的war包專案,jar包專案都是在同一個父模組下管理的(它們都是Maven專案)** (如果你有IDE,裝好外掛就用IDE建立吧,我個人不喜歡手動命令列建立)
1. 建立父專案
下圖中:框起來打勾這個會讓你跳過專案模式選擇,勾選對於建立專案沒有什麼影響,以後也許會轉一下Maven這方面的文章
POM包才能做父專案,謹記!!!!!
2. 子專案結構和建立
以下是我的結構分層,你也可以按你的想法來,最終目的是要方便自己開發。 test_parent(父專案) |----test_web(web專案) |----test_service(業務內容) |----test_framework(工具,框架封裝、配置) |----test_dao(資料持久層,DO也放這) |----test_controller(處理對映)
建立子專案直接右鍵父專案然後新建maven module ,也就是子模組
我們先建立web模組,這裡你可以勾選第一條然後建立簡單專案,
如果沒有勾選,那麼你要在下一步裡選擇 maven-achetype-webapp,這裡以簡單專案為例子
Group Id 和 version 都是繼承父專案的
一定要選擇war包打包,不然要重新把他構建成web專案。
如果你沒選war包: https://www.cnblogs.com/leonk...
最後點finish完成
點選生成Web描述檔案 (web.xml)
這樣就完成了Web模組的建立,剩下的其他專案都是同樣的步驟建立,都是選擇jar包,參考下圖:
3. 配置各模組的pom.xml
pom.xml記錄所需的jar包,模組聯絡,包資訊,打包引數等等資訊,在多模組裡我們要理清關係,不要重複引用 首先毫無疑問的是讓parent載入spring的jar包是最方便開發的,因為預設所有模組都繼承parent,所以子模組引用spring內容也方便。 其次配置檔案我們統一放在framework中進行管理。 那麼先來寫入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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>test_web</display-name> <context-param> <!-- 配置地址 --> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!-- spring-mvc.xml 配置地址 --> <param-value>classpath*:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</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> </web-app>
這裡可以看到我們寫了classpath*,原因是它能搜尋到專案目錄以外的Jar包下檔案
相關: http://www.cnblogs.com/wlgqo/...
web.xml詳解: https://blog.csdn.net/qq_3557...
web.xml是對WEB專案來說是必須的配置檔案,寫好了spring配置檔案的位置以後,就來新建2個spring配置檔案,新建的配置放在test_framework模組裡,路徑如下圖
spring-context.xml spring-mvc.xml
一個是spring-context.xml 也叫applicationContext.xml,是webApp的上下文配置,也可以理解為配置dao、service 通用bean的地方,但我們這裡使用的是註解掃描方式配置bean,所以就簡單許多,即便有工具存在,寫改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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 註解註冊 --> <!-- <context:annotation-config /> --> <context:component-scan base-package="com.test" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.RestController" /> </context:component-scan> </beans>
這裡要去掉對controller的掃描,applicationContext初始化的上下文載入的Bean是對於整個應用程式共享的,不管是使用什麼表現層技術,一般如DAO層、Service層Bean;
DispatcherServlet (下一個要配置的東西) 初始化的上下文載入的Bean是隻對Spring Web MVC有效的Bean,如Controller、HandlerMapping、HandlerAdapter等等,該初始化上下文應該只加載Web相關元件。
context:component-scan 的 base-package 值用來決定我們需要掃描的包的基礎名,
具體配置相關可以看: https://www.cnblogs.com/exe19...
而context:annotation-config/ 呢?其實也是spring為了方便我們開發者給我們提供的一個自動識別註解的配置,相關細節如下:
解釋說明: https://www.cnblogs.com/_popc...
兩條配置的區別和詮釋: https://www.cnblogs.com/leiOO...
下面是第二個配置檔案 spring-mvc.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 自動掃描的包名 --> <context:component-scan base-package="com.test.*.controller" > <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 預設的註解對映的支援 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> <bean class="org.springframework.http.converter.ResourceHttpMessageConverter" /> </mvc:message-converters> </mvc:annotation-driven> </beans>
包掃描沒什麼好說的,這裡還強調include了註解controller。
mvc:annotation-driven 是spring預設的註解驅動,這個配置項一口氣把一堆東西都給我們加進來了,但主要還是針對controller和處理請求的,具體的在下面文章中,因為加的內容有點多,所以這個留到後面研究,稍微理解作用就好:
相關文章 : https://blog.csdn.net/vicroad...
mvc:message-converters 顧名思義,就是用於處理請求訊息的,request content-header 會記錄請求的內容型別,根據這些型別,spring會把內容轉化成伺服器操作的物件,這裡的字串轉化是為了避免亂碼,我們指定了編碼格式。
相關文章: https://www.jianshu.com/p/2f6...
以上,我們就已經把最簡約的配置寫好了。接下來我們隨便寫一個controller試試
4.寫個Controller吧
根據之前寫好的controller的掃描包名,去我們test_controller模組裡建立一個controller
package com.test.hello.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/test") public class HelloController { @RequestMapping("/get") public String helloGet(@RequestParam String str) throws Exception { return str; } }
很簡單的一段返回請求字串的程式碼,現在一切就緒可以啟動伺服器了,配置好Tomcat就可以啟動了,右鍵test_web --> run as --> run on server 選擇建立好的tomcat容器,就可以啟動了。
接下來,訪問: localhost:8080/test/test/get?str=helloWorld
如果你使用eclipse啟動且沒有正常啟動,特別是出現嚴重錯誤時,請先檢查web.xml配置命名有沒有問題,然後再檢查test_web專案的assembly,這個會影響專案的釋出檔案,下圖所示,右鍵專案點properties,沒有test_framework的話就加入framework專案。
網站無響應,檢查一下tomcat的埠,預設是8080。404檢查程式碼的對映路徑。
5.結束語
第一次寫技術文章,記錄一下自己的學習過程,日後會以當前專案作為基礎,繼續記錄下自己遇到的問題和分享的知識,希望能幫助到一部分新手,此外,本篇文章中若有錯誤,歡迎指出,我會不斷更新文章誤點,不吝賜教。