ApplicationListener< ContextRefreshedEvent> 不建議
@Service public class SearchReceive implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { if (contextRefreshedEvent.getApplicationContext().getParent() == null) {//保证只执行一次 //需要执行的方法 } } }
@Component @Order(value = 1) public class AfterRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("执行方法"); } }
@Component @Order(value = 2) public class CommandLineRunnerImpl implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("执行方法"); } }
@Slf4j @Component public class RunnerTest implements ApplicationRunner, CommandLineRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("服务启动RunnerTest ApplicationRunner执行启动加载任务..."); } @Override public void run(String... args) throws Exception { System.out.println("服务启动RunnerTest CommandLineRunner 执行启动加载任务..."); } } }
以上是springboot專案啟動後的執行方法有哪些的詳細內容。更多資訊請關注PHP中文網其他相關文章!