force@autowiredspringboot和webflux

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

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

  1. @WebListener
  2. public class BagServletContextListener implements ServletContextListener {
  3. @Autowired
  4. private BagInjector injector;
  5. @Override
  6. public void contextInitialized(ServletContextEvent event) {
  7. this.injector.inject();
  8. }
  9. }

如何通过强制执行此组件的初始化 @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

  1. 注意:应用程序的打包是一场战争。
  2. 此组件缺少初始化导致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知道如何为您自动连线。
这样地:

  1. @ServletComponentScan
  2. @SpringBootApplication
  3. public class SpringBootApp {
  4. public static void main(String[] args) {
  5. SpringApplication.run(SpringBootApp.class, args);
  6. }
  7. }

相关问题