启动加上@EnableAsync ,需要执行异步方法上加入 @Async
@SpringBootApplication
@EnableAsync
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@RestController
public class AsyncController {
@Autowired
private AsyncService asyncService;
// @Async实现异步调用
@RequestMapping("/async")
public String Async() {
System.out.println("--------------1111--------------");
asyncService.Async();
System.out.println("--------------2222--------------");
return "Async";
}
}
@Service
public class AsyncServiceImpl implements AsyncService {
@Override
@Async
public void Async() {
System.out.println("--------------3333--------------");
}
}
代码托管:springboot_async