force@autowiredspringboot和webflux

f45qwnt8  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(203)

我的应用程序使用 spring bootwebflux 具有 tomcat 嵌入的
我使用第三个库,其中包含一些servlet侦听器。
侦听器属性 injector 属于 BagServletContextListener 启动应用程序时返回null。

@WebListener
public class BagServletContextListener implements ServletContextListener {
    @Autowired
    private BagInjector injector;

    @Override
    public void contextInitialized(ServletContextEvent event) {
        this.injector.inject();

    }
}

如何通过强制执行此组件的初始化 @bean 还是其他方式?
我的一部分 pom.xml ```

org.springframework.boot
spring-boot-starter-actuator


org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-test
test


org.springframework.kafka
spring-kafka


org.springframework.kafka
spring-kafka-test
test


org.springframework.boot
spring-boot-starter-webflux


org.springframework.boot
spring-boot-devtools
runtime
true


org.springframework.boot
spring-boot-starter-tomcat
provided

注意:应用程序的打包是一场战争。
此组件缺少初始化导致contextinitialized方法中出现空指针异常。 `[ERROR ] SRVE0283E: Exception caught while initializing context: java.lang.NullPointerException at com.devIo.ee.BagServletContextListener.contextInitialized(BagServletContextListener.java:33) at com.ibm.ws.webcontainer.webapp.WebApp.notifyServletContextCreated(WebApp.java:2391) at [internal classes]` 
4ngedf3f

4ngedf3f1#

您还没有告诉spring boot扫描您的 BagServletContextListener 班级。这个 @WebListener 这并不能实现。
添加 @ServletComponentScan 到您的springbootapplication类,以确保 BagInjector 被扫描-spring知道如何为您自动连线。
这样地:

@ServletComponentScan
@SpringBootApplication
public class SpringBootApp {

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

}

相关问题