在Spring Boot中,有些代碼是WEB功能,例如API等,但是有些邏輯是非WEB,啓動時就要調用並持續運行的,該如何加載自己的非WEB邏輯呢?
SpringBootApplication類實現CommandLineRunner
並覆蓋run()
方法
@SpringBootApplication
public class ZjkApplication implements CommandLineRunner{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// WEB
SpringApplication.run(ZjkApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
//your logic
System.out.println("into zjk run");
}
}
使用上面方式啓動的SPRING BOOT,即可以運行WEB又可以運行自己的邏輯