@Component
public class MyTask implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// 在这里编写要在应用程序启动后执行的任务
System.out.println("Hello, I am a task run by CommandLineRunner.");
}
}
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
// 项目启动时将demoService注入到方法参数并执行初始化操作
CommandLineRunner init(DemoService demoService) {
return (args) -> {
// do something with demoService
};
}
}