当前位置: 首页 > news >正文

【练手】自定义注解+AOP

在SpringBoot中实现自定义注解:( 声明注解的作用级别以及保留域 )

@Target({ElementType.METHOD,ElementType.PARAMETER}) //注解的作用级别
@Retention(RetentionPolicy.RUNTIME)                 //注解的保留域
public @interface Log {int value() default 99;
}

在SpringBoot中使用AOP:

<!--  先添加依赖  -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>
//然后写切面类
@Aspect
@Component
public class LogAspect {@AfterReturning("@annotation(controllerLog)")public void doAfterReturning(JoinPoint joinPoint, Log controllerLog){System.out.println(controllerLog.value());System.out.println("this is AfterReturning");}@AfterThrowing("@annotation(com.hcs.annatation.Log)")public void doAfterThrowing(){System.out.println("this is AfterThrowing");}}

最后,在表现层selectAll( )方法上使用自定义的注解进行测试:

@RestController
@RequestMapping("/users")
public class UserController {@GetMapping@Log(66)public void selectAll(){}}

控制台输出:
66
this is AfterReturning

http://www.lryc.cn/news/109464.html

相关文章:

  • QComboBox添加样式后,编辑栏背景一直白色问题解决方法。
  • vue动态绑定多个class以及带上三元运算或其他条件
  • Rpc原理
  • yapi容器化docker部署以及mongodb容器的持久化挂载异常问题
  • MyBatis-XML映射文件
  • C++类和对象入门(下)
  • 安卓:实现复制粘贴功能
  • jenkins pipeline项目
  • 机器学习笔记 - YOLO-NAS 最高效的目标检测算法之一
  • Grafana集成prometheus(3.Grafana添加promethus数据)
  • flutter开发实战-实现首页分类目录入口切换功能
  • 基于粒子群改进BP神经网络的时间序列预测,pso-bp时间序列预测
  • std::string和std::wstring无法前向声明
  • 论文阅读-Neighbor Contrastive Learning on Learnable Graph Augmentation(AAAI2023)
  • PostgreSql 进程及内存结构
  • Elasticsearch 常用 HTTP 接口
  • games106 homework1实现
  • Pytorch使用VGG16模型进行预测猫狗二分类
  • 安装nvm使用nvm管理node切换npm镜像后使用vue ui管理构建项目成功
  • 在线LaTeX公式编辑器编辑公式
  • 【C、C++】学习0
  • python GUI nicegui初识一(登录界面创建)
  • 【单片机】51单片机串口的收发实验,串口程序
  • 【bug】记录一次使用Swiper插件时loop属性和slidersPerView属性冲突问题
  • 云原生应用里的服务发现
  • 【零基础学Rust | 基础系列 | 基础语法】变量,数据类型,运算符,控制流
  • 运输层---概述
  • 高速公路巡检无人机,为何成为公路巡检的主流工具
  • 仓库管理系统有哪些功能,如何对仓库进行有效管理
  • Java 比Automic更高效的累加器