My application use spring boot and webflux with tomcat embedded.
I use a 3rd library that contains some servlet listeners.
A listener property injector of BagServletContextListener returns null when the app is started.
@WebListener
public class BagServletContextListener implements ServletContextListener {
@Autowired
private BagInjector injector;
@Override
public void contextInitialized(ServletContextEvent event) {
this.injector.inject();
}
}
How can I force initialization of this component through @bean or other way?
a piece of my 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>
Note: The packaging of app is a war.
The lack of initialization of this component cause null pointer exception in contextInitialized method.
[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]
You have not told Spring Boot to scan your
BagServletContextListenerclass. The@WebListenerdoes not accomplish that.Add
@ServletComponentScanto your SpringBootApplication class to ensure theBagInjectoris scanned - and Spring knows how to autowire it for you.Like this: