Modifying Spring Startup Post with Application
I used spring boot to do a trial service. It works fine when I run it using the java -jar DemoLibrary.war command on the command line. I got the correct message that "the library application is running".
I liked the below in the Appplication.java file;
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Application.class, args);
LogService.info(Application.class.getName(), "Library Application Has Started.");
}
}
When I run it in external tomcat, it starts up fine and works fine. But I don't see the same message that I no longer use this main method. I just see a message about starting a spring application.
Is there a way to change this message and give it how I want?
source to share
Add a ApplicationListener<ContextRefreshedEvent>
typed class and register it as @Bean
.
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public ApplicationListener<ContextRefreshedEvent> startupLoggingListener() {
return new ApplicationListener<ContextRefreshedEvent>() {
public void onApplicationEvent(ContextRefreshedEvent event) {
LogService.info(Application.class.getName(), "Library Application Has Started.");
}
};
}
}
Something like this should work in both situations without duplicating code (although the code is not complicated, but still).
source to share