springboot中aop的应用场景_Spring boot 使用AOP 通过注解处理404异常

这篇博客介绍了如何利用Springboot的AOP功能优化代码中的异常处理。作者通过创建自定义注解@NotFound,结合AspectJ的切点和通知,实现了一行代码自动检查SQL查询结果并抛出404异常的效果,减少了重复的空判断代码,提高了代码的整洁性和可维护性。
摘要由CSDN通过智能技术生成

这是一篇Spring boot aop的简单应用。

应用场景

我在我的应用中,如果尝试获取一个不存在的资源,例如GET /user/123,当不存在123这个用户时,我会返回404错误给前端。

以前的做法是在service层获取资源,资源为null时抛出NotFoundException异常。久而久之代码里就会充斥着大量的重复判断的代码,例如ArticleDO articleDO = articleMapper.getArticle(articleId);

if (articleDO == null) {

throw Exceptions.notFoundException;

}

这明显不美观,于是我通过aop的方式用一行注解@NotFound实现了上面的效果,只要SQL查询结果为null,即抛出异常。/**

* 获取文章详细

* @param id 文章id

* @return 文章详细

* @throws BaseNotFoundException not found

*/

@NotFound

@Select("SELECT * FROM db_article WHERE article_id=#{articleId}")

ArticleDO getArticle(Integer id) throws BaseNotFoundException;

引入包

org.springframework.boot

spring-boot-starter-aop

定义切点@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

public @interface NotFound {

Class> notFoundExceptionClz() default BaseNotFoundException.class;

}@Aspect

@Component

public class NotFoundAop {

@Pointcut("@annotation(nf)")

public void notFound(NotFound nf) {

}

}

以上是通过注解@annotation的方式定义了一个切点,除了使用注解,还可以使用execution的方式指定包、类、方法等。

spring AspectJ的Execution表达式

AOP的5种通知before(前置通知): 在方法开始执行前执行

after(后置通知): 在方法执行后执行

afterReturning(返回后通知): 在方法返回后执行

afterThrowing(异常通知): 在抛出异常时执行

around(环绕通知): 在方法执行前和执行后都会执行

执行顺序around > before > around > after > afterReturning

代码实现

因为要对返回值进行判断,所以用了@AfterReturning通知,returning定义的是方法返回值。@Aspect

@Component

public class NotFoundAop {

@Pointcut("@annotation(nf)")

public void notFound(NotFound nf) {

}

@AfterReturning(returning = "ret", pointcut = "notFound(nf)", argNames = "joinPoint,ret,nf")

public void doNotFound(JoinPoint joinPoint, Object ret, NotFound nf) throws Exception {

if (ret != null) {

return;

}

if (BaseNotFoundException.class.isAssignableFrom(nf.notFoundExceptionClz())) {

throw (BaseNotFoundException) nf.notFoundExceptionClz().getDeclaredConstructor().newInstance();

} else {

throw Exceptions.notFoundException;

}

}

}

我在@NotFound注解中加了notFoundExceptionClz参数,可以选填抛出的异常,不填则默认抛出Exceptions.notFoundException。这只是一个简单的不能再简单的demo,AOP的功能很强大,能做到的事情也非常的多。如果项目中出现了大量重复不美观的代码,就要考虑是否可以靠一些方式简化了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值