1. 程式人生 > >SpringBoot基礎篇(三)啟動載入資料CommandLineRunner詳解

SpringBoot基礎篇(三)啟動載入資料CommandLineRunner詳解

        SpringBoot應用程式在啟動時,會遍歷CommandLineRunner介面的例項並執行他們的run()方法。也可以利用@Order註解或者Order介面來規定所有CommandLineRunner例項的執行順序。

/**
 * 伺服器啟動時執行
*如果我們需要定義多個CommandLineRunner介面例項,用@Order指定先後順序
 */
@Component
@Order(value = 1)
public class StartRunning implements CommandLineRunner
{
   @Autowired
   private  DeptService deptService;
   @Autowired
   private YearService yearService;
@Autowired
private QuotaService quotaService;


    @Override
    public void run(String... args) throws Exception
    {
        System.out.println("============伺服器啟動時執行================");
        for (String arg:args){
            //args引數陣列是啟動傳入進來的
            System.out.println("========執行的引數====="+arg);
        }
        //初始化載入區域資訊資訊
        deptService.zTreeInit();
        //初始化年度資訊
       // yearService.yearListInit();
        yearService.treeInit();
        quotaService.ztreeInit();
    }

}