Spring Boot CommandLineRunner

CommandLineRunner

CommandLineRunner is an interface to run specific pieces of code when an application is started.

The interface gets called just before run() on the SpringBoot command SpringApplication.run().

CommandLine Runner can be implemented as a @Component or a @Bean

CommandLineRunner as a @Component

@Component
public class StartUpConfig implements CommandLineRunner {

    private static final Logger logger = LoggerFactory.getLogger(StartUpConfig.class);

    @Override
    public void run(String ...args) throws Exception {
        logger.info("Application started");
    }
}

CommandLineRunner as a @Bean

@Configuration
public class StartUpConfig {

    private static final Logger logger = LoggerFactory.getLogger(StartUpConfig);

    @Bean
    CommandLineRunner initDatabase() {
        return args -> {
            log.info("Application started");
        };
    }
}

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.