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 BagServletContextListener
class. The @WebListener
does not accomplish that.
Add @ServletComponentScan
to your SpringBootApplication class to ensure the BagInjector
is scanned - and Spring knows how to autowire it for you.
Like this:
@ServletComponentScan
@SpringBootApplication
public class SpringBootApp {
public static void main(String[] args) {
SpringApplication.run(SpringBootApp.class, args);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.