Spring boot 는 CommandLine Runner 를 통 해 시작 완료 후 작업 을 수행 합 니 다.

우 리 는 항상 이러한 수요 가 있 습 니 다. Spring boot 프로젝트 가 시 작 된 후에 초기 화 된 작업 과 같은 작업 을 바로 수행 해 야 합 니 다.
실현 하 는 방법 은 여러 가지 가 있 습 니 다. 오늘 저희 가 소개 한 것 은 CommandLine Runner 를 통 해 이 루어 지 는 것 입 니 다.
  • CommandLineRunner 인 터 페 이 스 를 실현 하고 @ Component 주석 추가
  • 그리고 run 방법 에서 구체 적 으로 실행 할 임 무 를 실현 합 니 다
  • 여러 퀘 스 트 가 있 고 선후 집행 순서 가 있 으 면 @ Order 로 설명 할 수 있 습 니 다. value 값 이 작 을 수록 우선 순위 가 높 습 니 다
  • 다음 두 가지 간단 한 작업 을 만 들 고 달 려 보 세 요.
    @Component
    @Order(value = 1) //        ,        
    public class MyRunner1 implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            System.out.println("MyRunner1");
        }
    }
    @Component
    @Order(value = 2) //        ,        
    public class MyRunner2 implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            System.out.println("MyRunner2");
        }
    }

    결 과 는 다음 과 같 습 니 다. 두 작업 은 MyApplication 이 시 작 된 후에 실행 되 고 MyRunner 1 이 먼저 실 행 됩 니 다.
    2020-08-21 15:21:49.169 custom-logback  INFO 19368 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 88888 (http) with context path ''
    2020-08-21 15:21:49.177 custom-logback  INFO 19368 --- [           main] com.yeyuanxinyi.MyApplication  : Started MyApplication in 14.903 seconds (JVM running for 19.368)
    MyRunner1
    MyRunner2

    좋은 웹페이지 즐겨찾기