- @PostConstruct annotation
- ApplicationReadyEvent event
- CommandLineRunner/ApplicationRunner接口
@Component public class StartUpInit { @Autowired private SomeService service; @PostConstruct public void init(){ // ... } }
@Component public class GeneralEventHandler { @EventListener public void handleApplicationReady(ApplicationReadyEvent event) { log.info("The application is ready to service requests.."); } }
Spring Boot provides two interfaces: CommandLineRunner and ApplicationRunner, which are used to do special processing when starting the application. These codes will be executed before the run() method of SpringApplication is completed.
It is usually used for special code execution before application startup, special data loading, garbage data cleaning, service discovery registration of microservices, notification after successful system startup, etc. Equivalent to Spring's ApplicationListener and Servlet's ServletContextListener.
The difference between CommandLineRunner and ApplicationRunner is that the parameters of the run() method are different.
(1) CommandLineRunner: The parameter is an array of strings
@Component public class CommandLineAppStartupRunner implements CommandLineRunner { private static final Logger logger = LoggerFactory.getLogger(CommandLineAppStartupRunner.class); @Override public void run(String... args) throws Exception { logger.info("Application started with command-line arguments: {} . \n To kill this application, press Ctrl + C.", Arrays.toString(args)); } }
(2) ApplicationRunner: Parameters are put into ApplicationArguments
to get parameters through getOptionNames(), getOptionValues(), getSourceArgs()
@Component public class AppStartupRunner implements ApplicationRunner { private static final Logger logger = LoggerFactory.getLogger(AppStartupRunner.class); @Override public void run(ApplicationArguments args) throws Exception { logger.info("Your application started with option names : {}", args.getOptionNames()); } }
It is also possible to implement both interfaces at the same time, but it is not necessary.
@Component public class StartupRunner implements CommandLineRunner, ApplicationRunner { private static final Logger logger = LoggerFactory.getLogger(CommandLineAppStartupRunner.class); @Override public void run(String... args) throws Exception { logger.info("Application started with command-line arguments: {} . \n To kill this application, press Ctrl + C.", Arrays.toString(args)); } @Override public void run(ApplicationArguments args) throws Exception { logger.info("Your application started with option names : {}", args.getOptionNames()); } }
It can also be defined via @Bean
@Configuration public class RunnerConfig { @Bean public CommandLineRunner runner(){ return new CommandLineRunner() { public void run(String... args){ System.out.println("CommandLineRunner run()"); } }; } }
(3) Set the execution order through @Order
@Component @Order(3) public class Runner1 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("Runner1 run()"); } } @Component @Order(2) public class Runner2 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("Runner2 run()"); } } @Component @Order(1) public class Runner3 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("Runner3 run()"); } }
(4) Bean injection
When CommandLineRunner is executed, Spring has been started internally, and Spring's Bean can be injected.
@Component public class StartupRunner implements CommandLineRunner { @Autowired private SampleService sampleService; @Override public void run(String... args) throws Exception { sampleService.executeSample(); } }