AsyncService.java
package com.qian.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
//告诉Spring这是一个异步的方法
@Async
public void hello(){
try{
Thread.sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("数据正在处理。。。");
}
}
@Async 告诉spring这是一个异步的方法
package com.qian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
//开启异步注解功能
@EnableAsync
@SpringBootApplication
public class SpringbootTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTestApplication.class, args);
}
}
@EnableAsync 开启异步注解功能
package com.qian.controller;
import com.qian.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController //返回字符串,用RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@RequestMapping("/hello")
public String hello(){
asyncService.hello();//停止三秒
return "OK";
}
}
//页面会即时刷新返回ok,但是后端会等待3秒后才输出“数据正在处理中”
相比于之前的Javaweb,springboot在邮件发送功能上同样简化了很多
首先导入相关依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-mailartifactId>
dependency>
#加密后的密码,从qq邮箱开启POP3/SMTP服务中拿到
spring.mail.password=fiqktykwcdpybdjc
spring.mail.username=1831213271@qq.com
spring.mail.host=smtp.qq.com
# 开启加密验证(QQ邮箱有的)
spring.mail.properties.mail.smtp.ssl.enable=true
@SpringBootTest
class SpringbootTestApplicationTests {
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
//一个简单的邮件发送
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setSubject("小狂神你好呀~"); //发送主题(标题)
mailMessage.setText("谢谢你"); //发送内容
mailMessage.setTo("1831213271@qq.com"); //发送给谁
mailMessage.setFrom("1831213271@qq.com"); //谁发送
mailSender.send(mailMessage);
}
@Test
void contextLoads2() throws MessagingException {
//一个复杂的邮件发送
MimeMessage mimeMessage = mailSender.createMimeMessage();
//组装~
//正文
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setSubject("小狂神你好呀~plus");
helper.setText("谢谢你的课程~
",true);
//附件
helper.addAttachment("3.jpg",new File("D:\\我的文件\\我的素材\\图片\\3.jpg"));
helper.addAttachment("1.jpeg",new File("D:\\我的文件\\我的素材\\图片\\1.jpeg"));
helper.setTo("1831213271@qq.com"); //发送给谁
helper.setFrom("1831213271@qq.com"); //谁发送
mailSender.send(mimeMessage);
}
然后邮箱就可以收到啦~
package com.qian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@EnableAsync //开启异步注解功能
@EnableScheduling // 开启定时功能的注解
@SpringBootApplication
public class SpringbootTestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootTestApplication.class, args);
}
}
@EnableScheduling // 开启定时功能的注解
package com.qian.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class ScheduledService {
//在一个特定的时间执行这个方法! Timer
//cron表达式
//秒 分 时 日 月 周几到周几 0-7意思是周日到周日就是每天
//0 15 10 ? * 1-6 每个月的周一到周六的10:15执行
@Scheduled(cron = "0/2 * * * * ?") //每天的15:24:00执行
public void hello(){
System.out.println("hello,你被执行了");
}
}
**@Scheduled(cron = “0/2 * * * * ?”) ** cron表达式