1.创建一个带线程内部类的组件
@Component
public class AccountQueueThread {
@Autowired
private SomeFeignClient someFeignClient;
@Autowired
private SomeService someService;
public void execute() {
new Worker().start();
}
private class Worker extends Thread {
@Override
public void run() {
try {
Thread.sleep(10000);//延迟10秒加载
} catch (InterruptedException e) {
e.printStackTrace();
}
doSomeThing
}
}
}
2.将创建的组件注入自启动服务
@Component
public class AccountQueueStartupRunner implements CommandLineRunner {
@Autowired
AccountQueueThread accountQueueThread;
@Override
public void run(String... args) throws Exception {
accountQueueThread.execute();
}
}