I try to implement my own ApplicationListener as follows:
package org.flowsen.listener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
public class EnvironmentFilterListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private final Log logger = LogFactory.getLog(getClass());
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent applicationEvent) {
logger.info("ApplicationEnvironmentPreparedEvent was thrown!");
}
}
After this I started my application with the following application class:
package org.flowsen;
import org.flowsen.listener.EnvironmentFilterListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class FlowsenApplication {
private static final Logger log = LoggerFactory.getLogger(FlowsenApplication.class);
@Value("${spring.application.name:Default Name}")
private String applicationName;
public static void main(String[] args) {
//SpringApplication.setAddCommandLineProperties(false);
SpringApplication.run(FlowsenApplication.class, args).addApplicationListener(new EnvironmentFilterListener());
//new SpringApplicationBuilder(FlowsenApplication.class)
// .listeners(new EnvironmentFilterListener())
// .build()
// .run(args);
}
@Bean
public CommandLineRunner appName() {
return (args) -> {
log.info("Application Name: " + applicationName);
};
}
}
I've I now run the application with the application class, the log message is not displayed because he never runs in the listener. I've I use the SpringApplicationBuilder (commented code) the log message appear.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.flowsen</groupId>
<artifactId>FlowsenTest</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Flowsen Test</name>
<description>Project for testing Flowsen stuff</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Comment From: snicoll
As noted in the Javadoc of SpringApplication.run
, it starts the application and return the ApplicationContext
. It turns out that the return type also has a addApplicationListener
method but that code sample basically adds the listener once the application has started.
Comment From: FlowsenAusMonotown
@snicoll So how can I call the addApplicationListener
then, when not by this way?
Comment From: snicoll
Via the builder as you've done or by using the SpringApplication
API properly, i.e.
SpringApplication application = new SpringApplication(FlowsenApplication.class);
application.addListeners(new EnvironmentFilterListener());
application.run(args)
If you have more questions please follow up on StackOverflow, as mentioned in the guidelines for contributing, we prefer to use GitHub issues only for bugs and enhancements.