创建项目

创建web项目,使用maven webapp模板进行构建,创建完成后,在pom中引入Spring MVC
依赖,如下:

<dependency>
    <!-- 引入SpringMVC依赖,使用版本为:5.0.5.RELEASE -->
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.0.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

web.xml配置

在web.xml配置文件中,配置Spring MVC Servlet控制器(DispacherServlet),如下:

<?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_3_1.xsd"
         version="3.1">
  <display-name>Archetype Created Web Application</display-name>
</web-app>

自定义WebApplicationInitializer

WebApplicationInitializer是SpringMVC核心初始化器,继承关系为:AbstractAnnotationConfigDispatcherServletInitializer-->AbstractDispatcherServletInitializer-->AbstractContextLoaderInitializer-->WebApplicationInitializer,自定义只需集成AbstractAnnotationConfigDispatcherServletInitializer即可,如下:

package com.github.dalianghe.config;

import org.springframework.lang.Nullable;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    /**
     *  返回Spring应用根容器中定义的beans,对应ContextLoaderListener,是Spring根容器
     * @return
     */
    @Nullable
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return null;
    }

    /**
     *  返回Spring MVC应用容器中定义的beans,对应DispatcherServlet中加载的bean
     *  Spring MVC容器是根容器的子容器,子容器可以看到根容器中定义的beans,反之不行
     * @return
     */
    @Nullable
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebConfig.class};
    }

    /**
     *  指定映射拦截URLs
     * @return
     */
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    /**
     *  通过重写此方法修改DispatcherServlet的名称,对应<servlet-name></servlet-name>标签
     * @return
     */
    @Override
    protected String getServletName() {
        return "dispatcher";
    }
}

创建WebMvc配置类

通过@EnableWebMvc注解开启Spring MVC特性,并且通过@ComponentScan指定扫描路径,在此类中配置视图解析器、静态资源处理等等,代码(最简)如下:

package com.github.dalianghe.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.github.dalianghe.controller")
public class WebConfig{
}

创建Controller类

创建Controller用于处理我们的请求,代码如下:

package com.github.dalianghe.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @GetMapping(value = "/test")
    public String test(){
        return "hello spring mvc annotation!";
    }

}

部署应用

本例通过maven插件进行部署,在pom文件中添加插件,如下:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <path>/</path>
                    <port>8080</port>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

访问应用

在浏览器地址栏输入如下地址:

http://localhost:8080/test

总结

本文以java配置的方式,通过WebApplicationInitializer配置Spring MVC(替换传统web.xml方式),实现了一个rest风格的服务,此方式是Spring3.1之后引入配置方式,使用Servlet3.0技术规范,在Servlet3.0+中web容器启动时,扫描类路径下所有的WebApplicationInitializer接口。

最后创建了qq群方便大家交流,可扫描加入,同时也可加我qq:276420284,共同学习、共同进步,谢谢!


dalianghe
95 声望29 粉丝

仰望星空,脚踏实地。