前言
相信在平常的項目中,大家對SpringBoot已經用的滾瓜爛熟了,現在準備寫一系列文章來記錄、重溫下知識體系,以查缺補漏。文筆有限,多多包涵!
進入SpringBoot之前,先來回顧下用XML的方式配置Spring。
一、項目搭建
-
創建maven項目xmlssm,不需要選模版
-
在pom.xml中加入
<packaging>war</packaging>
聲明打成war包 -
右擊項目選擇open module setting,然後在Module下點擊+號選擇Web,設置如下:
二、項目配置
-
在pom中加入SpringMVC依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.junya</groupId> <artifactId>xmlssm</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.1.10.RELEASE</version> </dependency> </dependencies> </project>
-
右鍵resources目錄,New -> XML Configuration File -> Spring Config,創建Spring的配置文件applicationContext.xml和SpringMVC的配置文件spring-servlet.xml。
注:Spring容器是SpringMVC容器的父容器,子容器可以訪問父容器,而父容器訪問不到子容器的東西,一般Spring父容器用來掃描除controller之外的東西。
-
新建controller和service包,在applicationContext.xml中配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <context:component-scan base-package="com.junya" use-default-filters="true"> <!-- 排除掉controller --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
Spring-servlet.xml中配置如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.junya" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <mvc:annotation-driven/> </beans>
-
在web.xml中加載兩個配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-servlet.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
三、寫restful接口測試
-
新建HelloService和HelloController類
package com.junya.service; import org.springframework.stereotype.Service; /** * @program com.junya * @description: * @author: zhangchao * @date: 2020/05/12 23:19 * @since: 1.0.0 */ @Service public class HelloService { public String sayHello() { return "你好啊!!"; } }
package com.junya.controller; import com.junya.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @program com.junya * @description: 測試 * @author: zhangchao * @date: 2020/05/12 23:14 * @since: 1.0.0 */ @RestController public class HelloController { @Autowired private HelloService helloService; @GetMapping("/hello") public String hello() { return helloService.sayHello(); } }
-
啓動Tomcat,訪問http://localhost:8080/hello
有亂碼?只需在@GetMapping裏改下請求的編碼:
@GetMapping(value = "/hello",produces = "text/html;charset=utf-8")
重啓Tomcat:
一切正常!