A Quick Intro to the SpringBootServletInitializer – 对SpringBootServletInitializer的快速介绍

最后修改: 2018年 4月 27日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll go through a quick introduction of the SpringBootServletInitializer.

在本教程中,我们将对SpringBootServletInitializer进行快速介绍。

This is an extension of WebApplicationInitializer which runs a SpringApplication from a traditional WAR archive deployed on a web container. This class binds Servlet, Filter and ServletContextInitializer beans from the application context to the server.

这是WebApplicationInitializer的扩展,从部署在Web容器上的传统WAR归档中运行SpringApplication。该类将ServletFilterServletContextInitializer Bean从应用上下文绑定到服务器上。

Extending the SpringBootServletInitializer class also allows us to configure our application when it’s run by the servlet container, by overriding the configure() method.

扩展SpringBootServletInitializer类还允许我们在Servlet容器运行时配置我们的应用程序,方法是覆盖configure()方法。

2. SpringBootServletInitializer

2.SpringBootServletInitializer

To get more practical, we’ll show an example of a main class that extends the Initializer class.

为了更加实用,我们将展示一个扩展Initializer类的主类的例子。

Our @SpringBootApplication class called WarInitializerApplication extends the SpringBootServletInitializer and overrides the configure() method. That method uses SpringApplicationBuilder to simply register our class as a configuration class of the application:

我们的@SpringBootApplication类名为WarInitializerApplication,扩展了SpringBootServletInitializer并重写了configure()方法。该方法使用SpringApplicationBuilder来简单地注册我们的类作为应用程序的配置类。

@SpringBootApplication
public class WarInitializerApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(
      SpringApplicationBuilder builder) {
        return builder.sources(WarInitializerApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication sa = new SpringApplication(
          WarInitializerApplication.class);
        sa.run(args);
    }

    @RestController
    public static class WarInitializerController {

        @GetMapping("/")
        public String handler() {
           // ...
        }
    }
}

Now, if we package our application as a WAR, we’ll be able to deploy it on any web container in a traditional way, which will also execute the logic we added in the configure() method.

现在,如果我们把我们的应用程序打包成WAR,我们将能够以传统的方式在任何Web容器上部署它,这也将执行我们在configure()方法中添加的逻辑。

If we want to package it as a JAR file, then we’ll need to add the same logic to the main() method so that the embedded container can pick it up as well.

如果我们想把它打包成JAR文件,那么我们就需要在main()方法中添加同样的逻辑,以便嵌入式容器也能接收它。

3. Conclusion

3.结论

In this article, we introduced the SpringBootServletInitializer and demonstrated how we can use it to run Spring Boot applications from a classical WAR archive.

在这篇文章中,我们介绍了SpringBootServletInitializer,并演示了如何使用它从经典的WAR存档中运行Spring Boot应用程序。

The complete source code for the example is available over on GitHub. This is a Maven-based project, so it can be imported and used as-is.

该例子的完整源代码可在GitHub上找到这是一个基于Maven的项目,因此可以按原样导入和使用。