原文网址:Spring之AOP系列--将类上的注解作为切点(用@Pointcut)_IT利刃出鞘的博客-CSDN博客
简介
说明
本文介绍Spring(SpringBoot)的AOP的用法:自定义注解,在Controller的类上使用此注解,然后在AOP中获得注解和方法的相关信息,并获取返回值信息。
本文使用@Pointcut+@Before+@AfterReturning。
方案介绍
AOP可将切点设为注解类,例如,若想控制Controller,只需切@RestController,方法有:
法1:@Pointcut结合@within(推荐)
@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
法2:@Before或@Around,结合@within
@Before("@within(restController)")
public void before(JoinPoint joinPoint, RestController restController)
基础代码
项目结构、pom.xml、公共代码等与此文一样:Spring之AOP系列--将方法上的注解做为切点(用@Before)_IT利刃出鞘的博客-CSDN博客
效果展示
测试1:添加数据(有返回值)
后端结果
++++++++++++++++++++++++ before ++++++++++++++++++++++++++++++
------------------ joinPoint ------------------
execution(User com.example.demo.controller.UserController.testAdd(User))
------------------ 方法信息 ------------------
joinPoint.getTarget().toString() : com.example.demo.controller.UserController@1bc3bb5
methodSignature.getName() : testAdd
method.getName() : testAdd
method.getReturnType().getName() : com.example.demo.entity.User
------------------ 类的ApiLog注解数据 ------------------
tag: 用户接口
------------------ 类的Api注解数据 ------------------
tags:用户; value:
------------------ 方法注解数据 ----------------
操作为:添加数据
------------------ 入参数据 ------------------
User(id=12, userName=Tony, note=abc)
id是:12; 名字是:Tony; 备注是:abc
#################################################
+++++++++++++++++++++ afterReturning +++++++++++++++++++++++++
------------------ joinPoint ------------------
execution(User com.example.demo.controller.UserController.testAdd(User))
------------------ 返回值 --------------------
{"id":12,"userName":"Tony","note":"abc"}
++++++++++++++++++++++++ after ++++++++++++++++++++++++++++++
------------------ joinPoint ------------------
execution(User com.example.demo.controller.UserController.testAdd(User))
测试2:删除数据(无返回值)
后端结果:
++++++++++++++++++++++++ before ++++++++++++++++++++++++++++++
------------------ joinPoint ------------------
execution(void com.example.demo.controller.UserController.delete(User))
------------------ 方法信息 ------------------
joinPoint.getTarget().toString() : com.example.demo.controller.UserController@1bc3bb5
methodSignature.getName() : delete
method.getName() : delete
method.getReturnType().getName() : void
------------------ 类的ApiLog注解数据 ------------------
tag: 用户接口
------------------ 类的Api注解数据 ------------------
tags:用户; value:
------------------ 方法注解数据 ----------------
操作为:删除数据
------------------ 入参数据 ------------------
User(id=1212, userName=Peter, note=abc)
id是:1212; 名字是:Peter; 备注是:abc
#################################################
+++++++++++++++++++++ afterReturning +++++++++++++++++++++++++
------------------ joinPoint ------------------
execution(void com.example.demo.controller.UserController.delete(User))
------------------ 返回值 --------------------
null
++++++++++++++++++++++++ after ++++++++++++++++++++++++++++++
------------------ joinPoint ------------------
execution(void com.example.demo.controller.UserController.delete(User))
AOP代码
上边是文章的部分内容,为便于维护,全文已转移到此网址:Spring之AOP-将类上的注解作为切点(用@Pointcut) - 自学精灵