190316-Spring MVC之基於xml配置的web應用構建
直接用SpringBoot構建web應用可以說非常非常簡單了,在使用SpringBoot構建後端服務之前,一直用的是Spring + SpringMVC基於xml的配置方式來玩的,所以在正式進入SpringBoot Web篇之前,有必要看一下不用SpringBoot應該怎麼玩的,也因此方便凸顯SpringBoot的優越性
I. Web 構建
1. 專案依賴
我們選擇使用傳統的SpringMVC + Tomcat/Jetty 執行war包方式來執行任務,建立一個maven專案之後,先新增上基本的依賴
<artifactId>201-mvc-xml</artifactId> <!-- 注意這一行,我們指定war包 --> <packaging>war</packaging> <properties> <spring.version>5.1.5.RELEASE</spring.version> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
2. 專案結構
對於web專案,和我們傳統的不一樣的地方在於,會多一個 webapp
目錄,在這個目錄的 WEB-INF
資料夾下,會存有幾個必要的配置檔案
圖中的三個目錄,都屬於比較重要的
- java : 存放原始碼
- resources: 專案資原始檔存放地
- webapp: web的配置檔案,資原始檔預設存放地
3. 配置檔案說明
java和resources這兩個目錄沒啥好說的,主要來看一下webapp下面的三個xml配置檔案
a. web.xml
在我們使用xml配置的生態體系中,這個配置檔案至關重要;本節說到SpringMVC構建的應用,是在Servlet的生態上玩耍的;而web.xml這個配置檔案,比如我們常見的Servlet定義,filter定義等等,都在這xml檔案中
例項如下
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/j2ee/web-app_3_1.xsd" version="3.1"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 解決亂碼的問題 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <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> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
上面的配置中,定義了 DispatcherServlet
的名字為 mvc-dispatcher
,根據規範,會有一個叫做 mvc-dispatcher-servlet.xml
的配置檔案,其中的配置將應用於 DispatcherServlet
的上下文
b. mvc-dispatcher-servlet.xml
這個檔案主要可以用來定義Servlet相關的配置資訊,比如檢視解析,資源路徑指定等;一個最簡單的配置如下
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:beans="http://www.springframework.org/schema/mvc" xmlns="http://www.springframework.org/schema/beans" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--指定掃描的包路徑,自動註冊包含指定註解的物件到Spring容器,幷包含了 context:annotation-config 的作用--> <context:component-scan base-package="com.git.hui.spring"/> </beans>
在 web.xml
中, context:component-scan
非常非常重要,用來指定自動掃描並註冊bean到容器的包路徑,上面這一行配置,簡單來講可以認為做了下面幾件事情
- 掃描包
com.git.hui.spring
下所有的類,如果類上有@Component
,@Service
,@Repository
,@Contorller
,@RestContorller
,@Configuration
等註解,會例項化為bean物件,並註冊到Spring容器中 - 其次就是實現DI的功能,實現bean的依賴注入
接下來看一下,如果不加上面這一行,也想實現對應的效果改怎樣配置呢?
<!-- 這個使用來啟用註冊的Bean,簡單來講就是使Ioc工作起來 --> <context:annotation-config/> <bean name="printServer" class="com.git.hui.spring.PrintServer"/> <bean name="helloRest" class="com.git.hui.spring.HelloRest"/>
原始碼後面會給出,首先是主動定義兩個bean,其中 helloRest
為Controller, printServer
為一個Service,並被注入到helloRest中
如果只定義了兩個bean,而不加上 <context:annotation-config/>
,則HelloRest中的printService會是null,演示如下圖
此外,如果用了舊的Spring版本,直接用前面的配置,可能依然無法訪問web服務,這個時候有必要加一下下面的註解; 對於使用aop,希望使用cglib代理的,需要如下配置
<!-- 支援mvc註解--> <mvc:annotation-driven/> <!-- 使用cglib實現切面代理 --> <aop:aspectj-autoproxy proxy-target-class="true"/>
額外說明:現在基本上不怎麼用xml配置了,有更簡單的註解方式,上面的配置內容瞭解即可
c. applicationContext.xml
前面的截圖中,還有個配置檔案,這個是幹嘛的呢?
DispatchServlet載入包含在web元件中的bean(如mapper,Controller,ViewResolver);我們應用中,還有些其他的Spring Bean(比如其他rpc訪問的服務bean代理,db驅動元件等)則更多的是放在這個配置檔案中定義
當然這個裡面最簡單的配置內容就是啥都沒有,比如我們的demo工程
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>
4. 例項程式碼
配置完了之後,我們簡單的定義一個reset服務用來測試,比如一個簡單dean物件和一個簡單的Controller
簡單的bean物件
@Component public class PrintServer { public void print() { System.out.println(System.currentTimeMillis()); } }
Controller如下
@RestController public class HelloRest { @Autowired private PrintServer printServer; @ResponseBody @GetMapping("hello") public String sayHello(HttpServletRequest request) { printServer.print(); return "hello, " + request.getParameter("name"); } }
5. 測試
上面我們的web應用就搭建完畢了,然後就是把它部署起來,看下能不能愉快的玩耍了;我們有兩個方法
方法一:tomcat方式
mvn clean package -DskipTests=true
方法二:jetty方式
前面一種方式,有很多公司的服務是這麼玩的,將服務達成war包丟到tomcat中,然後服務上線;然而在本地開發測試時,這樣有點麻煩(當然可以通過idea配置tomcat除錯法,個人感覺,依然麻煩)
我們使用jetty來玩耍就很簡單了,首先在pom中新增配置,引入jetty外掛
<build> <finalName>web-mvc</finalName> <plugins> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.12.RC2</version> <configuration> <httpConnector> <port>8080</port> </httpConnector> </configuration> </plugin> </plugins> </build>
然後啟動方式可以使用命令: mvn jetty:run
, 也可以使用idea,如下,直接雙擊執行或者右鍵選擇debug模式啟動
然後我們愉快的啟動測試過程如下
到此,一個基於 Spring + SpringMVC + Jetty + xml配置的web應用就搭建起來了;下一篇我們將講一下,純java註解方式,拋棄xml配置又可以怎樣搭建一個web應用
II. 其他
0. 專案
- 工程: https://github.com/liuyueyi/spring-boot-demo
- 專案: https://github.com/liuyueyi/spring-boot-demo/blob/master/spring/201-mvc-xml
1. 一灰灰Blog
- 一灰灰Blog個人部落格 https://blog.hhui.top
- 一灰灰Blog-Spring專題部落格 http://spring.hhui.top
一灰灰的個人部落格,記錄所有學習和工作中的博文,歡迎大家前去逛逛
2. 宣告
盡信書則不如,以上內容,純屬一家之言,因個人能力有限,難免有疏漏和錯誤之處,如發現bug或者有更好的建議,歡迎批評指正,不吝感激
- 微博地址:小灰灰Blog
- QQ: 一灰灰/3302797840
3. 掃描關注
一灰灰blog
知識星球
打賞 如果覺得我的文章對您有幫助,請隨意打賞。