使用Java类代替web-xml

2022-12-21 21:16:06 浏览数 (1)

# 使用 Java 类代替 web.xml 启动 Spring MVC

在 servlet3.0 当中支持使用 Java 的类来配置启动容器,使用方法如下面的 Java 代码所示,可以作为模板使用:

代码语言:javascript复制
package top.dimples.config;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * @author dimples_yj
 * @since 2020-09-09
 * 当容器启动时,会调用该类中的onStartup方法
 */
public class WebInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        // 获取SpringMVC的上下文环境
        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        // 将SpringMVC的配置文件加载到上下文环境
        ctx.register(WebMvcConfig.class);
        // 设置servletContext上下文环境
        ctx.setServletContext(servletContext);
        // 设置SpringMVC的转发器
        DispatcherServlet dispatcherServlet = new DispatcherServlet(ctx);
        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcherServlet",dispatcherServlet);
        // 设置路径
        servlet.addMapping("/");
        // 设置启动级别
        servlet.setLoadOnStartup(1);
    }
}

代码当中的WebMvcConfig文件

代码语言:javascript复制
package top.dimples.config;

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

/**
 * @author dimples_yj
 * @since 2020-09-09
 */
@Configuration
@EnableWebMvc
@ComponentScan("top.dimples.controller")
public class WebMvcConfig {

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }
}

上述代码引入的各个 jar 包如下所示,使用 Gradle 作为构建工具

代码语言:javascript复制
plugins {
    id 'java'
    id 'war'
}

group 'org.example'
version '1.0-SNAPSHOT'


sourceCompatibility = 1.8

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'org.springframework', name: 'spring-context', version: '5.2.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-web', version: '5.2.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc', version: '5.2.6.RELEASE'
    providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.11.1'
    compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.11.1'

}

0 人点赞