@Async注解可以用在方法上,也可以用在类上,用在类上,对类里面所有方法起作用
@Service
public class AsynchronousService{
@Async
public void springAsynchronousMethod()
{
longTimeMethod();
}
}
其他类调用这个方法。
这里注意,一定要其他的类,如果在同类中调用,是不生效的。具体原因,可以去学习一下Spring AOP的原理
@Autowired
private AsynchronousService asynchronousService;
public void useAsynchronousMethod(){
//我们需要执行的代码1
asynchronousService.springAsynchronousMethod();
//我们需要执行的代码2
}
原文:https://www.cnblogs.com/eternityz/p/12238830.html
网友评论