手摸手,一起重温SpringBoot 2知识点(一)准备工作之回顾XML方式配置Spring

前言

相信在平常的项目中,大家对SpringBoot已经用的滚瓜烂熟了,现在准备写一系列文章来记录、重温下知识体系,以查缺补漏。文笔有限,多多包涵!

进入SpringBoot之前,先来回顾下用XML的方式配置Spring。

一、项目搭建

  1. 创建maven项目xmlssm,不需要选模版
    image-20200512215024629
    image-20200512215152031

  2. 在pom.xml中加入<packaging>war</packaging>声明打成war包

    image-20200512220001404

  3. 右击项目选择open module setting,然后在Module下点击+号选择Web,设置如下:
    image-20200512221952423
    image-20200512222640094

二、项目配置

  1. 在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>
    
  2. 右键resources目录,New -> XML Configuration File -> Spring Config,创建Spring的配置文件applicationContext.xml和SpringMVC的配置文件spring-servlet.xml。

    注:Spring容器是SpringMVC容器的父容器,子容器可以访问父容器,而父容器访问不到子容器的东西,一般Spring父容器用来扫描除controller之外的东西。

  3. 新建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>
    
  4. 在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接口测试

  1. 新建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();
        }
    
    }
    
  2. 启动Tomcat,访问http://localhost:8080/hello

    image-20200512235037438

    有乱码?只需在@GetMapping里改下请求的编码:

    @GetMapping(value = "/hello",produces = "text/html;charset=utf-8")
    

    重启Tomcat:

    image-20200512235639159

    一切正常!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值