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();
    }

}

猜你喜欢

转载自blog.csdn.net/u013089490/article/details/84614270