Java 强制@Autowired spring boot和webFlux

Java 强制@Autowired spring boot和webFlux,java,spring-boot,tomcat,autowired,servletcontextlistener,Java,Spring Boot,Tomcat,Autowired,Servletcontextlistener,我的应用程序使用spring-boot和webflux嵌入tomcat 我使用第三个库,其中包含一些servlet侦听器 当应用程序启动时,BagServletContextListener的侦听器属性injector返回null @WebListener public class BagServletContextListener implements ServletContextListener { @Autowired private BagInjector injecto

我的应用程序使用
spring-boot
webflux
嵌入
tomcat

我使用第三个库,其中包含一些servlet侦听器

当应用程序启动时,
BagServletContextListener
的侦听器属性
injector
返回null

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

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

    }
}
如何通过
@bean
或其他方式强制初始化此组件

一段我的
pom.xml

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <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.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

org.springframework.boot
弹簧靴起动器执行器
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧起动试验
测试
org.springframework.kafka
春天卡夫卡
org.springframework.kafka
弹簧卡夫卡试验
测试
org.springframework.boot
弹簧启动器webflux
org.springframework.boot
弹簧靴开发工具
运行时
真的
org.springframework.boot
弹簧启动机tomcat
假如
注意:应用程序的打包是一场战争

此组件缺少初始化导致contextInitialized方法中出现空指针异常


[ERROR]SRVE0283E:初始化上下文时捕获异常:com.devIo.ee.BagServletContextListener.contextInitialized(BagServletContextListener.java:33)处的java.lang.NullPointerException位于com.ibm.ws.webcontainer.webapp.webapp.notifyServletContextCreated(webapp.java:2391)处的[内部类]

您没有告诉Spring Boot扫描您的
BagServletContextListener
类。
@WebListener
无法实现这一点

@ServletComponentScan
添加到SpringBootApplication类中,以确保扫描
BagInjector
,并且Spring知道如何为您自动连接它

像这样:

@ServletComponentScan
@SpringBootApplication
public class SpringBootApp {

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

}

不幸的是,它没有工作,因为注入器为空,所以异常不断发生:(“只有在使用嵌入式web服务器时才执行扫描。”@Chuie:你试过用嵌入式服务器运行你的应用程序吗?@Piotr P.Karwasz我使用tomcat嵌入式服务器,因为根据你的说法,这个类部署在
WEB-INF/classes
中,Spring没有实例化它,tomcat有。tomcat只能注入在JNDI注册的资源(通过
@Inject
@Resource
注释)。我不知道我是否需要创建WEB-INF/classes。我的项目不包含它。你说
BagServletContextListener
是你项目中的一个java文件:java文件被编译并复制到WAR文件的
WEB-INF/classes
文件夹中,并被扫描以获取Servlet 3.0注释。另一方面,你的Maven依赖者CIE被复制到
WEB-INF/lib
中,并且不会扫描注释。这就是为什么Tomcat发现并实例化您的类,但无法插入
BagInjector