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?

+3


source to share


2 answers


You can use onStartup

something like:



@Override
public void onStartup(final ServletContext servletContext) throws ServletException {
    LogService.info(Application.class.getName(), "Library Application Has Started.");
    super.onStartup(servletContext);
}

      

+1


source


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).

+5


source







All Articles