簡體   English   中英

強制@Autowired spring 啟動和 webFlux

[英]Force @Autowired spring boot and webFlux

我的應用程序使用spring boot和嵌入webfluxtomcat

我使用包含一些 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>

注意:應用程序的打包是一場戰爭。

該組件缺少初始化導致contextInitialized方法中的null指針異常。

[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]

您還沒有告訴 Spring Boot 掃描您的BagServletContextListener class。 @WebListener沒有做到這一點。

@ServletComponentScan添加到您的 SpringBootApplication class 以確保BagInjector被掃描 - Spring 知道如何為您自動裝配它。

像這樣:

@ServletComponentScan
@SpringBootApplication
public class SpringBootApp {

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM