CommandLineRunner接口

如果希望在SpringBoot应用启动时进行一些初始化操作,可以选择使用CommandLineRunner来进行处理。
我们只需要实现CommandLineRunner接口,把实现类注入到spring中。在重写的方法run中写相关的代码。
这样就会在应用启动的时候执行对应的代码。

  1. package com.liyadong.runner;
  2. import org.springframework.boot.CommandLineRunner;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class TestRunner implements CommandLineRunner {
  6. @Override
  7. public void run(String... args) throws Exception {
  8. System.out.println("程序初始化");
  9. }
  10. }