SpringBoot3进阶用法
标签:切面.调度.邮件.监控;
一、简介
在上篇《SpringBoot3基础》中已经完成入门案例的开发和测试,在这篇内容中再来看看进阶功能的用法;
主要涉及如下几个功能点:
调度任务:在应用中提供一定的轻量级的调度能力,比如方法按指定的定时规则执行,或者异步执行,从而完成相应的代码逻辑;
邮件发送:邮件作为消息体系中的渠道,是常用的功能;
应用监控:实时或定期监控应用的健康状态,以及各种关键的指标信息;
切面编程:通过预编译方式和运行期动态代理实现程序中部分功能统一维护的技术,可以将业务流程中的部分逻辑解耦处理,提升可复用性;
二、工程搭建
1、工程结构

2、依赖管理
<!-- 基础框架依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- 应用监控组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- 切面编程组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- 邮件发送组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>${spring-boot.version}</version>
</dependency>
这里再细致的查看一下各个功能的组件依赖体系,SpringBoot只是提供了强大的集成能力;

3、启动类
注意在启动类中使用注解开启了异步EnableAsync和调度EnableScheduling的能力;
@EnableAsync
@EnableScheduling
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
三、切面编程
1、定义注解
定义一个方法级的注解;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface DefAop {
/**
* 模块描述
*/
String modelDesc();
/**
* 其他信息
*/
String otherInfo();
}
2、注解切面
在切面中使用Around环绕通知类型,会拦截到DefAop注解标记的方法,然后解析获取各种信息,进而嵌入自定义的流程逻辑;
@Component
@Aspect
public class LogicAop {
private static final Logger logger = LoggerFactory.getLogger(LogicAop.class) ;
/**
* 切入点
*/
@Pointcut("@annotation(com.boot.senior.aop.DefAop)")
public void defAopPointCut() {
}
/**
* 环绕切入
*/
@Around("defAopPointCut()")
public Object around (ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object result = null ;
try{
// 执行方法
result = proceedingJoinPoint.proceed();
} catch (Exception e){
e.printStackTrace();
} finally {
// 处理逻辑
buildLogicAop(proceedingJoinPoint) ;
}
return result ;
}
/**
* 构建处理逻辑
*/
private void buildLogicAop (ProceedingJoinPoint point){
// 获取方法
MethodSignature signature = (MethodSignature) point.getSignature();
Method reqMethod = signature.getMethod();
// 获取注解
DefAop defAop = reqMethod.getAnnotation(DefAop.class);
String modelDesc = defAop.modelDesc() ;
String otherInfo = defAop.otherInfo();
logger.info("DefAop-modelDesc:{}",modelDesc);
logger.info("DefAop-otherInfo:{}",otherInfo);
}
}
四、调度任务
1、异步处理
1.1 方法定义
通过Async注解标识两个方法,方法在执行时会休眠10秒,其中一个注解指定异步执行使用asyncPool线程池;
@Service
public class AsyncService {
private static final Logger log = LoggerFactory.getLogger(AsyncService.class);
@Async
public void asyncJob (){
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
log.info("async-job-01-end...");
}
@Async("asyncPool")
public void asyncJobPool (){
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
log.info("async-job-02-end...");
}
}
1.2 线程池
定义一个ThreadPoolTaskExecutor线程池对象;
@Configuration
public class PoolConfig {
@Bean("asyncPool")
public Executor asyncPool () {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 线程池命名前缀
executor.setThreadNamePrefix("async-pool-");
// 核心线程数5
executor.setCorePoolSize(5);
// 最大线程数10
executor.setMaxPoolSize(10);
// 缓冲执行任务的队列50
executor.setQueueCapacity(50);
// 线程的空闲时间60秒
executor.setKeepAliveSeconds(60);
// 线程池对拒绝任务的处理策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 线程池关闭的时等待所有任务都完成再继续销毁其他的Bean
executor.setWaitForTasksToCompleteOnShutdown(true);
// 设置线程池中任务的等待时间
executor.setAwaitTerminationSeconds(300);
return executor;
}
}
1.3 输出信息
从输出的日志信息中可以发现,两个异步方法所使用的线程池不一样,asyncJob采用默认的cTaskExecutor线程池,asyncJobPool方法采用的是async-pool线程池;
[schedule-pool-1] c.boot.senior.schedule.ScheduleService : async-job-02-end...
[cTaskExecutor-1] c.boot.senior.schedule.ScheduleService : async-job-01-end...
2、调度任务
2.1 调度配置
通过实现SchedulingConfigurer接口,来修改调度任务的配置,这里重新定义任务执行的线程池;
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(5));
}
}
2.2 调度方法
通过Scheduled注解来标记方法,基于定时器的规则设定,来统一管理方法的执行时间;
@Component
public class ScheduleJob {
private static final Logger log = LoggerFactory.getLogger(ScheduleJob.class);
private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
/**
* 上一次开始执行时间点之后10秒再执行
*/
@Scheduled(fixedRate = 10000)
private void timerJob1(){
log.info("timer-job-1:{}",format.format(new Date()));
}
/**
* 上一次执行完毕时间点之后10秒再执行
*/
@Scheduled(fixedDelay = 10000)
private void timerJob2(){
log.info("timer-job-2:{}",format.format(new Date()));
}
/**
* Cron表达式:每30秒执行一次
*/
@Scheduled(cron = "0/30 * * * * ?")
private void timerJob3(){
log.info("timer-job-3:{}",format.format(new Date()));
}
}
五、邮件发送
1、邮件配置
采用QQ邮箱来模拟邮件的发送方,需要先开启smtp邮件传输协议,在QQ邮箱的设置/账户路径下,并且获取相应的授权码,在项目的配置中使用;

spring:
application:
name: boot-senior
# 邮件配置
mail:
host: smtp.qq.com
port: 465
protocol: smtps
username: 邮箱账号
password: 邮箱授权码
properties:
mail.smtp.ssl.enable: true
2、方法封装
定义一个简单的邮件发送方法,并且可以添加附件,是常用的功能之一;另外也可以通过Html静态页渲染,再转换为邮件内容的方式;
@Service
public class SendMailService {
@Value("${spring.mail.username}")
private String userName ;
@Resource
private JavaMailSender sender;
/**
* 带附件的邮件发送方法
* @param toUsers 接收人
* @param subject 主题
* @param content 内容
* @param attachPath 附件地址
* @return java.lang.String
* @since 2023-07-10 17:03
*/
public String sendMail (String[] toUsers,String subject,
String content,String attachPath) throws Exception {
// MIME邮件类
MimeMessage mimeMessage = sender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
// 邮件发送方From和接收方To
helper.setFrom(userName);
helper.setTo(toUsers);
// 邮件主题和内容
helper.setSubject(subject);
helper.setText(content);
// 邮件中的附件
File attachFile = ResourceUtils.getFile(attachPath);
helper.addAttachment(attachFile.getName(), attachFile);
// 执行邮件发送命令
sender.send(mimeMessage);
return "send...mail...sus" ;
}
}
测试结果

六、应用监控
1、监控配置
在springboot的actuator组件中,可以通过提供的Rest接口,来获取应用的监控信息;
# 应用监控配置
management:
endpoints:
web:
exposure:
# 打开所有的监控点
include: "*"
base-path: /monitor
endpoint:
health:
enabled: true
show-details: always
beans:
enabled: true
shutdown:
enabled: true
2、相关接口
2.1 Get类型接口:主机:端口/monitor/health,查看应用的健康信息,三个核心指标:status状态,diskSpace磁盘空间,ping检查;
{
/* 状态值 */
"status": "UP",
"components": {
/* 磁盘空间 */
"diskSpace": {
"status": "UP",
"details": {
"total": 250685575168,
"free": 112149811200,
"threshold": 10485760,
"path": "Path/butte-spring-parent/.",
"exists": true
}
},
/* Ping检查 */
"ping": {
"status": "UP"
}
}
}
2.2 Get类型接口:主机:端口/monitor/beans,查看bean列表;
{
"contexts": {
"boot-senior": {
"beans": {
"asyncPool": {
"scope": "singleton",
"type": "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor",
"resource": "class path resource [com/boot/senior/schedule/PoolConfig.class]"
},
"asyncService": {
"scope": "singleton",
"type": "com.boot.senior.schedule.AsyncService$$SpringCGLIB$$0"
}
}
}
}
}
2.3 Post类型接口:主机:端口/monitor/shutdown,关闭应用程序;
{
"message": "Shutting down, bye..."
}
七、参考源码
文档仓库:
https://gitee.com/cicadasmile/butte-java-note
源码仓库:
https://gitee.com/cicadasmile/butte-spring-parent
SpringBoot3进阶用法的更多相关文章
- Django框架学习-Model进阶用法
Model进阶用法 回顾 访问外键 访问多对多关系 更改数据库结构 当处理数据库结构改变时,需要注意到几点: 增加字段 首先在开发环境中: 再到产品环境中: 删除字段 删除多对多字段 删除model ...
- canvas图形处理和进阶用法
前面的话 上一篇博客介绍了canvas基础用法,本文将更进一步,介绍canvas的图形处理和进阶用法 图形变换 图形变换是指用数学方法调整所绘形状的物理属性,其实质是坐标变形.所有的变换都依赖于后台的 ...
- 前端自动化测试神器-Katalon进阶用法
前言 上一篇介绍了Katalon的基础用法,本篇继续介绍一些进阶的用法. Keyword 和 Method Call Statement Keyword Keyword就是自定义方法,该方法在当前项目 ...
- SpringBoot进阶用法-随笔
SpringBoot进阶用法 实现setApplicationContext //实现ApplicationContextAware接口,重写setApplicationContext方法 publi ...
- CocoaPods学习系列4——进阶用法
这篇文章,记录一下CocoaPods的进阶用法. 进阶用法主要体现在.podspec文件和Podfile的配置上. .podspec文件的进阶配置 以官方的一个.podspec文件示例细说: Pod: ...
- (数据科学学习手札61)xpath进阶用法
一.简介 xpath作为对网页.对xml文件进行定位的工具,速度快,语法简洁明了,在网络爬虫解析内容的过程中起到很大的作用,除了xpath的基础用法之外(可参考我之前写的(数据科学学习手札50)基于P ...
- webpack进阶用法你都get到了么?
如何消除无用代码:打包自己的私有js库:实现代码分割和动态import提升初次加载速度:配置eslint规范团队代码规范:打包异常抓捕你都get到了么? 摇树优化:Tree Shaking webpa ...
- xpath进阶用法
一.简介 xpath作为对网页.对xml文件进行定位的工具,速度快,语法简洁明了,在网络爬虫解析内容的过程中起到很大的作用,除了xpath的基础用法之外xpath中还存在着非常之多的进阶用法,本文将对 ...
- ASP.NET Core 6框架揭秘实例演示[14]:日志的进阶用法
为了对各种日志框架进行整合,微软创建了一个用来提供统一的日志编程模式的日志框架.<日志的基本编程模式>以实例演示的方式介绍了日志的基本编程模式,现在我们来补充几种"进阶" ...
- Spring Data JPA系列3:JPA项目中核心场景与进阶用法介绍
大家好,又见面了. 到这里呢,已经是本SpringData JPA系列文档的第三篇了,先来回顾下前面两篇: 在第1篇<Spring Data JPA系列1:JDBC.ORM.JPA.Spring ...
随机推荐
- 自制ASP.NET 本地授权文件
asp.net登录时验证本地ini文件是否正确,主要步骤. 1.导入myini.DLL文件. 下载地址:http://yunpan.cn/cKw9kHJUk9Ui8 提取码 6631 2.添加引用 ...
- RMQ问题ST表
稀疏表(Sparse Table表) 解决静态RMQ,区间最值查询问题的数据结构,树状数组(BIT)解决动态前缀和问题的数据结构: 例:https://www.luogu.org/problemnew ...
- 2021-08-15:给定一个字符串Str,返回Str的所有子序列中有多少不同的字面值。
2021-08-15:给定一个字符串Str,返回Str的所有子序列中有多少不同的字面值. 福大大 答案2021-08-15: 返回值=上+新-修正. 时间复杂度:O(N) 空间复杂度:O(N). 代码 ...
- 2021-10-13:单词接龙。字典 wordList 中从单词 beginWord 和 endWord 的 转换序列 是一个按下述规格形成的序列:序列中第一个单词是 beginWord 。序列中最后
2021-10-13:单词接龙.字典 wordList 中从单词 beginWord 和 endWord 的 转换序列 是一个按下述规格形成的序列:序列中第一个单词是 beginWord .序列中最后 ...
- 2021-09-10:给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那两个整数,并返回它们的数组下标。你可以假设每种输入只会对应一个答案
2021-09-10:给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那两个整数,并返回它们的数组下标.你可以假设每种输入只会对应一个答案, ...
- javascript中的错误类型
javascript 中的错误类型: SyntaxError TypeError ReferenceError RangeError URLError Error SyntaxError 语法错误 / ...
- 研究NIST FIPS 199 - 安全分类的标准
NIST FIPS 199 - 安全分类的标准 FIPS199是在2004年2月发布的,这是一份古老的文件,但在实施信息安全时应首先遵循,无论你准备遵守哪种安全标准.常见的安全标准有:CIS.ISO2 ...
- GPT-4多态大模型研究
1.概述 GPT-4是OpenAI最新的系统,能够产生更安全和更有用的回应.它是一个大型的多模态模型(接受图像和文本输入,输出文本),在各种专业和学术的基准测试中展现了人类水平的表现.例如,它在模拟的 ...
- 【最佳实践】如何设计 RESTful API ?
良好的 API 设计是一个经常被提及的话题,特别是对于那些试图完善其 API 策略的团队来说.一个设计良好的 API 的好处包括:改进开发者体验.更快速地编写文档以及更高效地推广你的 API.但是,到 ...
- Java(数组使用、Arrays、稀疏数组)
1.数组的使用 For-Each循环 int[] arrays = {1,2,3,4,5}; //打印全部的数组元素 JDK1.5 没有下标 for (int array : arrays) { Sy ...