[英]Force @Autowired spring boot and webFlux
我的應用程序使用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>
注意:應用程序的打包是一場戰爭。
該組件缺少初始化導致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.