前言
相信在平常的项目中,大家对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:
一切正常!