【问题标题】:WebApplicationInitializer don't work in springbootWebApplicationInitializer 在 Spring Boot 中不起作用
【发布时间】:2022-08-02 10:18:27
【问题描述】:

我的 WebConfig 代码,它实现了 WebApplicationInitializer

public class WebConfig implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        System.out.println(\"on startup\");
    }
}

我的应用程序代码

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }

}

我的 pom.xml

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

但是我在控制台中看不到“启动时”。任何人都可以帮助我吗?谢谢。而且我不想说太多,但是stackoverflow看起来像是阻止我发布问题....

    标签: java


    【解决方案1】:

    这实际上是一个有意的设计决定。搜索算法 容器使用是有问题的。时也会引起问题 您想开发一个可执行的 WAR,因为您经常需要一个 WAR 的 javax.servlet.ServletContainerInitializer 不是 运行 java -jar 时执行。

    见 org.springframework.boot.context.embedded.ServletContextInitializer 一个适用于 Spring Beans 的选项。

    --https://github.com/spring-projects/spring-boot/issues/321#issuecomment-34353800

    另见https://github.com/spring-projects/spring-boot/issues/522

    【讨论】:

      猜你喜欢
      • 2019-04-16
      • 2015-02-17
      • 1970-01-01
      • 2018-09-24
      • 2020-10-31
      • 2019-11-25
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多