springboot在@Scheduled注解方法去调用另外@component注解过的一个类的一个@Async方法,能实现异步吗?
`
//伪代码
@Component
public class AsyncTask {
@Async
public void a(String str){
System.out.println("执行异步a方法"+"内容为:"+str);
}
}
@Component
public class SpringBootTask {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Autowired
AsyncTask asynctask;
@Autowired
BService bService;
@Scheduled(cron="0 30 00 * * ?")
public void timerJob(){
List<String> list = b.service.getXXList();
//这个定时器大概是要让a()执行多次,但是内容不同。
//我是想定时器任务里能不能新开线程去处理数据
for(String str:list){
asynctask.a(str);
}
}
}
`
当然是可以的,但是你只是简单的输出字符串可能看不出什么效果。