向来我都不做代码过多的解释,因为我的代码注释 已!经!足!够!详!细 ! 了!
不过还是得提一下这俩注解,
@ComponentScan(“com.async.service”) 这个是告诉全世界,我即将开启异步线程的业务方法是哪个
@EnableAsync 这个! 必须有! 告诉全世界允许我使用异步线程
好了,接下来我们创建一下业务层的东西吧:
AsyncTestService:
package com.async.service;
public interface AsyncTestService {
/**
- 这里将会在impl里标注为异步任务,在执行此方法的时候,会单独开启线程来执行
*/
void function1() throws InterruptedException;
void function2();
}
AsyncTestServiceImpl:
package com.async.service.impl;
import com.async.service.AsyncTestService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
public class AsyncTestServiceImpl implements AsyncTestService {