Spring-aop切面环绕通知

server/2024/9/23 17:56:24/

1.pom引入

<!-- 切面依赖-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2.定义注解:AroundAnnotation

java">import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AroundAnnotation {String value() default "";
}

3.定义切面:Aspect

java">import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;@Aspect
@Component
public class AroundAop {//  execution(* study.mapper.*(..)) @annotation(study.aop.AroundAnnotation)@Pointcut("execution(* study.mapper.*.*(..)) ")public void aspect(){}//定义@Around增强,poincut连接点使用@annotation(xxxx)进行定义 @annotation(study.aop.AroundAnnotation)@Around("@annotation(annotation)")public Object processTx(ProceedingJoinPoint joinPoint, AroundAnnotation annotation) throws Throwable {long startTime = System.currentTimeMillis();try {//AroundAnnotation annotation =joinPoint.getTarget().getClass().getAnnotation(AroundAnnotation.class);String value = annotation.value();System.out.println("arount 进入-----" + value);// 执行目标方法Object result = joinPoint.proceed();long endTime = System.currentTimeMillis();System.out.println("执行目标方法:"+ joinPoint.getSignature() + " executed in " + (endTime - startTime) + "ms");return result;} catch (Throwable throwable) {throwable.printStackTrace();} finally {System.out.println("arount 出去 -----" );}return null;}@Before("aspect()")public void before(JoinPoint joinPoint) {Object[] args = joinPoint.getArgs();    //获取方法入参System.out.println("before -- 原方法的入参是:"+args);System.out.println("before -- 原方法执行前会先执行我!!参数:" + args[0]);}@AfterReturning("aspect()")public void after(JoinPoint joinPoint) {Object[] args = joinPoint.getArgs();    //获取方法入参System.out.println("AfterReturning -- 原方法执行后会执行我!!参数:" + args);}}

4.使用切面注解

java">
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import study.aop.AroundAnnotation;
import study.entity.UserInfo;@Mapper
public interface UserMapper extends BaseMapper<UserInfo> {@AroundAnnotation("AAAA")@Select("select * from t_user where id = #{id}")public UserInfo selectByIdFromMaster(@Param("id") Integer id);}

5.执行结果


http://www.ppmy.cn/server/27014.html

相关文章

leetcode40

给定一个候选人编号的集合 candidates 和一个目标数 target &#xff0c;找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的每个数字在每个组合中只能使用 一次 。 注意&#xff1a;解集不能包含重复的组合。 示例 1: 输入: candidates [10,1,2,7,6…

python项目入门新手攻略

最近工作需要接手了代码量比较大的python开发的项目&#xff0c;平时写python不多&#xff0c;记录一下如何熟悉项目。 分析调用流程-pycallgraph 因为代码量比较大&#xff0c;所以希望通过工具生成代码调用流程&#xff0c;因此用到了pycallgraph。 pycallgraph&#xff0…

CSS之显示覆盖内容(z-index)

前言&#xff1a; 我们有的时候&#xff0c;希望下方的内容能够显示到上方&#xff0c;达到类似于多个图层的效果&#xff0c;此时我们可以利用z-index这个属性。 介绍&#xff1b; z-index属性值是用来设置元素的堆叠顺序(元素层级)。 覆盖原则&#xff1a; <1>特殊…

Microsoft Universal Print 与 SAP 集成教程

引言 从 SAP 环境打印是许多客户的要求。例如数据列表打印、批量打印或标签打印。此类生产和批量打印方案通常使用专用硬件、驱动程序和打印解决方案来解决。 Microsoft Universal Print 是一种基于云的打印解决方案&#xff0c;它允许组织以集中化的方式管理打印机和打印机驱…

docker-compose单机容器集群编排工具

一 docker-compose 简介 使用一个Dockerfile模板文件可以定义一个单独的应用容器&#xff08;可实现在单机上对容器集群编排管理 的工具&#xff09;&#xff0c;如果需要定义多个容器就需要服务编排。服务编排有很多种技术方案&#xff0c;给大家介绍 Docker 官方产品 Dock…

[华为OD] C卷 5G网络 现需要在某城市进行5G网络建设,已经选取N个地点设置5G基站 200

题目 现需要在某城市进行5G网络建设&#xff0c;已经选取N个地点设置5G基站&#xff0c;编号固定为1到N,接 下来需要各个基站之间使用光纤进行连接以确保基站能互联互通&#xff0c;不同基站之间架设光纤的成 本各不相同&#xff0c;且有些节点之间已经存在光纤相连&#…

品牌百度百科词条需要什么资料?

品牌百度百科词条是一个品牌的数字化名片&#xff0c;更是品牌历史、文化、实力的全面展现。 作为一个相当拿得出手的镀金名片&#xff0c;品牌百度百科词条创建需要什么资料&#xff0c;今天伯乐网络传媒就来给大家讲解一下。 一、品牌基本信息&#xff1a;品牌身份的明确 品…

C语言字符串(0基础到深入剖析)---字符串系列合集(函数+指针+数组)

前言 本篇旨在帮助不了解字符串或者逻辑梳理不够透彻的伙伴们理出一条脉络。选择能看懂的部分即可&#xff0c;建议收藏&#xff0c;后期学习完C语言方便回顾。 适用范围&#xff1a;0基础C语言&#xff08;刚学字符串&#xff09;- 学过函数 - 学过指针 ---大致了解了数据内…