# spring-boot-init-run **Repository Path**: fellowfun110/spring-boot-init-run ## Basic Information - **Project Name**: spring-boot-init-run - **Description**: SpringBoot启动后,执行初始化方法 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2022-03-31 - **Last Updated**: 2022-05-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 介绍 spring boot项目,有时我们希望在项目启动的时候,执行一些初始化方法,以下介绍几种实现方式 # 示例 ### 1. CommandLineRunner ```java @Component public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("我来自:CommandLineRunner"); } } ``` ### 2. ApplicationRunner ```java @Component public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("我来自:ApplicationRunner"); } } ``` ### 3. PostConstruct ```java @Configuration public class MyPostConstruct { @PostConstruct public void init() { System.out.println("我来自:PostConstruct"); } } ``` ### 4. EventListener ```java @Configuration public class MyEventListener { @EventListener public void init(ContextRefreshedEvent event) { System.out.println("我来自:EventListener"); } } ```