Spring中基于注解的AOC
AOP注意事项
切面类和目标类都需要交给IOC容器管理
切面类必须通过@Aspect注解标识为一个切面
在spring的配置文件中设置<aop:aspectj-autoproxy/>
开启基于注解的AOP功能
<!--扫描路径-->
<contest:component-scan base-package="com.javasm.spring.aop.annotation"></contest:component-scan>
<!--开启基于注解的AOP功能-->
<aop:aspectj-autoproxy/>
* 1.在切面中,需要通过指定的注解将方法标识为通知方法
* @Before:前置通知的注解,在目标方法执行之前执行
// @Before("execution(public int com.javasm.spring.aop.annotation.CalculatorImpl.add(int ,int ))")
@Before("pointCut()")
public void beforeAdviceMethod(JoinPoint joinPoint) {
//获取连接点所对应方法的签名信息
Signature signature = joinPoint.getSignature();
//获取连接点所对应方法的参数
Object[] args = joinPoint.getArgs();
System.out.println("LoggerAspect,方法:" + signature.getName() + ",参数:" + Arrays.toString(args));
}
* @After:后置通知,在目标对象方法的finally子句中执行的
@After("pointCut()")
public void afterAdviceMethod(JoinPoint joinPoint) {
//获取连接点所对应方法的签名信息
Signature signature = joinPoint.getSignature();
System.out.println("LoggerAspect,方法:" + signature.getName() + ",执行完毕");
}
* @AfterReturning:返回通知,在目标对象方法返回值之后执行
* 在返回通知中若要获取目标对象方法的返回值,
* 只需要通过@AfterReturning注解的returning属性
* 就可以将通知方法的某个参数指定为接受目标对象方法的返回值的参数
@AfterReturning(value = "pointCut()", returning = "result")
public void afterReturningAdviceMethod(JoinPoint joinPoint, Object result) {
//获取连接点所对应方法的签名信息
Signature signature = joinPoint.getSignature();
System.out.println("LoggerAspect,方法" + signature.getName() + "结果:" + result);
}
* @AfterThrowing:异常通知(例外通知),在目标对象方法的catch子句中执行
* 在异常通知中若要获取目标对象方法的异常,
* 只需要通过@AfterThrowing注解的throwing属性
* 就可以将通知方法的某个参数指定为接受目标对象方法出现的异常的参数
@AfterThrowing(value = "pointCut()", throwing = "ex")
public void afterThrowingAdviceMethod(JoinPoint joinPoint, Exception ex) {
//获取连接点所对应方法的签名信息
Signature signature = joinPoint.getSignature();
//获取异常信息
System.out.println("LoggerAspect,方法" + signature.getName() + ",异常:" + ex);
}
2.切入点表达式:设置在标识通知的注解的value属性中
* "execution(public int com.javasm.spring.aop.annotation.CalculatorImpl.add(int ,int ))"
* "execution(* com.javasm.spring.aop.annotation.CalculatorImpl.*(..))"
* 第一个*表示任意的访问修饰符和返回值类型
* 第二个*表示类中任意的方法
* 。。表示任意参数列表
* 在类的地方也能使用*,表示包下所有的类
* 3.重用切入点表达式
* //@Pointcut声明一个公共的切入点表达式
* @Pointcut("execution(* com.javasm.spring.aop.annotation.CalculatorImpl.*(..))")
* public void pointCut(){}
* 使用方式@Before("pointCut()")
* 4.获取连接点的信息
* 在通知方法的参数位置,
* 来设置JoinPoint类型的参数,
* 就可以获取连接点所对应方法的信息
* //获取连接点所对应方法的签名信息
* Signature signature = joinPoint.getSignature();
* //获取连接点所对应方法的参数
* Object[] args = joinPoint.getArgs();
*5.环绕通知(类似动态代理)
@Around("pointCut()")
//环绕通知的方法的返回值一定要和目标对象方法的返回值保持一致
public Object aroundAdviceMethod(ProceedingJoinPoint joinPoint) {
Object result=null;
try {
System.out.println("环绕通知-->前置通知");
//表示目标对象方法的执行
result= joinPoint.proceed();
System.out.println("环绕通知-->返回通知");
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("环绕通知-->异常通知");
} finally {
System.out.println("环绕通知-->后置通知");
}
return result;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!