Spring WebApplicationInitializer

来源:这里教程网 时间:2026-02-17 21:31:16 作者:

Spring WebApplicationInitializer 教程展示了如何使用 WebApplicationInitializer 以编程方式引导 Spring Web 应用。

Spring 是用于创建企业应用的流行 Java 应用框架。

WebApplicationInitializer

WebApplicationInitializer用于引导 Spring Web 应用。 WebApplicationInitializer注册一个 Spring DispatcherServlet并创建一个 Spring Web 应用上下文。 通常,开发人员使用AbstractAnnotationConfigDispatcherServletInitializer(它是WebApplicationInitializer的实现)来创建 Spring Web 应用。

传统上,基于 Servlet 的 Java Web 应用使用web.xml文件来配置 Java Web 应用。 从 Servlet 3.0 开始,可以通过 Servlet 上下文侦听器以编程方式创建 Web 应用。

Spring WebApplicationInitializer示例

以下应用使用WebApplicationInitializer创建 Spring Web 应用。

pom.xmlsrc├───main│   ├───java│   │   └───com│   │       └───zetcode│   │           ├───config│   │           │       MyWebInitializer.java│   │           │       WebConfig.java│   │           └───controller│   │                   MyController.java│   └───resources└───test    └───java

这是项目结构。

pom.xml

<?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.zetcode</groupId>    <artifactId>mockmvcex</artifactId>    <version>1.0-SNAPSHOT</version>    <packaging>war</packaging>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <maven.compiler.source>11</maven.compiler.source>        <maven.compiler.target>11</maven.compiler.target>        <spring-version>5.1.3.RELEASE</spring-version>    </properties>    <dependencies>        <dependency>            <groupId>ch.qos.logback</groupId>            <artifactId>logback-classic</artifactId>            <version>1.2.3</version>        </dependency>        <dependency>            <groupId>javax.servlet</groupId>            <artifactId>javax.servlet-api</artifactId>            <version>4.0.1</version>            <scope>provided</scope>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-webmvc</artifactId>            <version>5.1.3.RELEASE</version>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-war-plugin</artifactId>                <version>3.2.2</version>            </plugin>        </plugins>    </build></project>

在pom.xml文件中,我们具有以下依存关系:logback-classic javax.servlet-api和spring-webmvc。

com/zetcode/config/MyWebInitializer.java

package com.zetcode.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;public class MyWebInitializer implements WebApplicationInitializer {    @Override    public void onStartup(ServletContext servletContext) throws ServletException {        var ctx = new AnnotationConfigWebApplicationContext();        ctx.register(WebConfig.class);        ctx.setServletContext(servletContext);        var servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));        servlet.setLoadOnStartup(1);        servlet.addMapping("/");    }}

MyWebInitializer实现WebApplicationInitializer接口。 引导代码在onStartup()方法中。

var ctx = new AnnotationConfigWebApplicationContext();ctx.register(WebConfig.class);ctx.setServletContext(servletContext);

我们创建一个AnnotationConfigWebApplicationContext并向register()注册一个 Web 配置文件。 我们将应用上下文与 Servlet 上下文绑定。

var servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));servlet.setLoadOnStartup(1);servlet.addMapping("/");

在这里,我们注册一个 Spring DispatcherServlet,它是 Spring Web 应用的前端控制器。

com/zetcode/config/WebConfig.java

package com.zetcode.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.zetcode")public class WebConfig {}

WebConfig通过@EnableWebMvc启用 Spring MVC 注解,并为com.zetcode软件包配置组件扫描。

com/zetcode/controller/MyController.java

package com.zetcode.controller;import org.springframework.http.MediaType;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class MyController {    @GetMapping(value = "/", produces = MediaType.TEXT_PLAIN_VALUE)    public String home() {        return "This is home page";    }}

MyController是一种 RESTful 控制器,具有一个映射。

$ curl localhost:8080This is home page

我们使用curl工具连接到主页。

在本教程中,我们使用WebApplicationInitializer创建了一个简单的 Spring Web 应用。

相关推荐