4-02.AspectJ-基于注解的讲解
MyAspect.java
package com.exp.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; @Component @Aspect public class MyAspect { //声明一个公共的切入点 @Pointcut("execution(* com.exp.service.UserServiceImpl.*(..))") public void myPointcut(){} @Before("myPointcut()") public void myBefore(JoinPoint jp){ System.out.println("1.前置通知..." + jp.getSignature().getName());//连接点方法名 } /** * 后置通知获取service方法执行后的返回值 * Object retValue:service方法执行的返回值,如果写了返回值,需要在xml中配置returning * @param jp */ // <aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="retValue"/> @AfterReturning(pointcut = "myPointcut()",returning = "retValue") public void myAfterReturning(JoinPoint jp,Object retValue){ System.out.println("3.后置通知..." + jp.getSignature().getName()); System.out.println("返回值:" + retValue); } @Around("myPointcut()") public Object myAround(ProceedingJoinPoint pjp) throws Throwable { System.out.println("2.环绕通知...开启事务..." + pjp.getSignature().getName()); //放行 Object retObj = pjp.proceed(); System.out.println("4.环绕通知....提交事务..."); return retObj; } @AfterThrowing(pointcut = "myPointcut()",throwing = "e") public void myAfterThrowing(JoinPoint jp,Throwable e){ System.out.println("异常通知..." + jp.getSignature().getName() + "===" + e.getMessage() ); } @After("myPointcut()") public void myAfter(JoinPoint jp){ System.out.println("最终通知..." + jp.getSignature().getName()); } }
bean02.xml
<?xml version="1.0" encoding="UTF-8"?> <!--xmlns xml namespace:xml命名空间--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p ="http://www.springframework.org/schema/p" xmlns:context ="http://www.springframework.org/schema/context" xmlns:aop ="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置UserService--> <bean id="userService" class="com.exp.service.UserServiceImpl"></bean> <!-- 配置切面对象--> <bean id="myAspect" class="com.exp.aspect.MyAspect"></bean> <!-- 配置 aop --> <aop:config> <!-- aop:指定切面--> <aop:aspect ref="myAspect"> <!--定义一个切入点--> <aop:pointcut id="myPointcut" expression="execution(* com.exp.service.UserServiceImpl.*(..))"/> <!-- 配置前置通知...--> <aop:before method="myBefore" pointcut-ref="myPointcut" /> <!-- 配置后置通知...--> <aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut" returning="retValue"/> <!--配置环绕通知--> <aop:around method="myAround" pointcut-ref="myPointcut"></aop:around> <!-- 配置异常通知 throwing="e" 值,是方法的参数名 --> <aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="e"/> <!--配置最终通知:不管有没有异常,最终通知都会执行--> <aop:after method="myAfter" pointcut-ref="myPointcut"></aop:after> </aop:aspect> </aop:config> </beans>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?