企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Spring `WebApplicationInitializer`教程 > [http://zetcode.com/spring/webapplicationinitializer/](http://zetcode.com/spring/webapplicationinitializer/) 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 应用。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ ├───config │ │ │ MyWebInitializer.java │ │ │ WebConfig.java │ │ └───controller │ │ MyController.java │ └───resources └───test └───java ``` 这是项目结构。 `pom.xml` ```java <?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` ```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()`方法中。 ```java var ctx = new AnnotationConfigWebApplicationContext(); ctx.register(WebConfig.class); ctx.setServletContext(servletContext); ``` 我们创建一个`AnnotationConfigWebApplicationContext`并向`register()`注册一个 Web 配置文件。 我们将应用上下文与 Servlet 上下文绑定。 ```java var servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.setLoadOnStartup(1); servlet.addMapping("/"); ``` 在这里,我们注册一个 Spring `DispatcherServlet`,它是 Spring Web 应用的前端控制器。 `com/zetcode/config/WebConfig.java` ```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` ```java package com.zetcode.controller; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @GetMapping(value = "/", produces = MediaType.TEXT_PLAIN_VALUE) public String home() { return "This is home page"; } } ``` `MyController`是一种 RESTful 控制器,具有一个映射。 ```java $ curl localhost:8080 This is home page ``` 我们使用`curl`工具连接到主页。 在本教程中,我们使用`WebApplicationInitializer`创建了一个简单的 Spring Web 应用。 您可能也对这些相关教程感兴趣: [Spring `@GetMapping`教程](/spring/getmapping/), [Spring `DefaultServlet`教程](/spring/defaultservlet/), [Spring Web 应用简介](/articles/springwebfirst/)和 [Java 教程](/lang/java/)。