java - 强制 @Autowired 弹簧靴和 webFlux
问题描述
我的应用程序使用并spring boot
嵌入。webflux
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
<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 方法出现空指针异常。
[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
类。@WebListener
没有做到这一点。
添加@ServletComponentScan
到您的 SpringBootApplication 类以确保BagInjector
已扫描 - 并且 Spring 知道如何为您自动装配它。
像这样:
@ServletComponentScan
@SpringBootApplication
public class SpringBootApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class, args);
}
}
推荐阅读
- laravel - Laravel 在数据库中显示 0 而不是保存的值
- python-3.x - 使用 Python 3 将数组标准化为特定的最大值
- angular - 以角度捕获从 REST 返回的字符串。我已经实现了它,但我想知道它是否以正确的方式完成
- javascript - 我不明白如何编写函数:((
- c++ - C++ 线程安全队列
- android - 使用接口处理 AsyncTask Activity 在 Android Studio 中显示为错误,但在运行时不显示
- javascript - 使用基本方法Javascript让单个数字相互相乘直到达到单个数字
- spring-boot - Spring JPA - 在运行时替换数据源
- python - Python 请求未正确下载 pdf
- java - Apache Beam KafkaIO 处理卡在 readfromkafka