Springboot 开启异步任务Async,邮件发送任务,定时任务
异步任务
1.主启动类开启异步注解

2.service目录下开启异步任务注解
@Service public class AsyncService { @Async//异步任务注解的标志 public void hello(){ try { Thread.sleep(3000); } catch (InterruptedException e){ e.printStackTrace(); } System.out.println("数据正在处理中..."); } }
3.controller目录下的方法调用service目录下的方法
@RestController public class AsyncController { @Autowired AsyncService asyncService; @RequestMapping("/h") public String hello(){ asyncService.hello();//停止3秒 return "ok"; } }
4.运行结果

Springboot邮件发送简单任务
1.导入依赖并配置properties文件
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.6.3</version>
</dependency>

2.测试代码

3.运行结果

4.实现复杂的邮件发送
代码

运行结果:

定时任务
1.主启动类开启定时任务的注解

2.测试

运行结果:

浙公网安备 33010602011771号