Spring 学习——Spring AOP——AOP配置篇Advice(无参数传递)
声明通知Advice
- 配置方式(以前置通知为例子)
- 方式一
<aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut>
<aop:before method="aspectBefore" pointcut-ref="ikPoint"></aop:before>
</aop:aspect>
</aop:config>- 优点:前置通知、后置通知、环绕通知使用同一个切点时,配置一个<aop:poincut />即可,方便配置。
- 方式二
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:before method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before> </aop:aspect> </aop:config>
- 优点:前置通知、后置通知、环绕通知使用不同切点时,不需要配置<aop:poincut />元素,可以直接在<aop:before />元素内配置切点。
- Next
- 方式一
- 前置通知(Before Advice)
- 在切入点时机事务执行之前,执行通知
- 方式一
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut> <aop:before method="aspectBefore" pointcut-ref="ikPoint"></aop:before> </aop:aspect> </aop:config>
- 方式二
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:before method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before> </aop:aspect> </aop:config>
- Next
- 后置通知(After Running Advice)
- 在切入点时机事务执行之后,执行通知(前提:切入点执行时没有异常,否则不会执行)
- 方式一
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut> <aop:after-returning method="aspectBefore" pointcut-ref="ikPoint"></aop:before> </aop:aspect> </aop:config>
- 方式二
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:after-returning method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before> </aop:aspect> </aop:config>
- Next
- 抛出异常通知(After Throwing Advice)
- 若在切入点时机执行时抛出异常,执行通知。(执行异常通知,则不会执行后置通知)
- 方式一
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut> <aop:after-throwing method="aspectBefore" pointcut-ref="ikPoint"></aop:before> </aop:aspect> </aop:config>
- 方式二
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:after-throwing method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before> </aop:aspect> </aop:config>
- Next
- 后通知(After Ending Advice)
- 在切入点时机执行之后,执行通知。(切入点执行时,无论正常执行,还是抛出异常,都会执行通知,相当于try_catch_finally)。
- 方式一
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut> <aop:after method="aspectBefore" pointcut-ref="ikPoint"></aop:before> </aop:aspect> </aop:config>
- 方式二
<aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:after method="aspectBefore" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:before> </aop:aspect> </aop:config>
- Next
- 无论切面是否出现异常,后通知动作正常执行
- 环绕通知(Around Advice)
- 环绕通知在切入点时机前后都可以执行通知。(在切面类中的通知方法中,默认存在参数ProceedingJoinPoint pj)
- 方式一
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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" >
<bean id="ikAspect" class="com.jing.spring.aop.IKAspect"></bean>
<bean id="ikAspectBiz" class="com.jing.spring.aop.IKAspectBiz"></bean>
<aop:config>
<aop:aspect id="ikAspectAop" ref="ikAspect">
<aop:pointcut id="ikPoint" expression="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:pointcut>
<aop:around method="aspectAround" pointcut-ref="ikPoint"></aop:around>
</aop:aspect>
</aop:config>
</beans>package com.jing.spring.aop; import org.aspectj.lang.ProceedingJoinPoint; /**
*切面类
*/ public class IKAspect { public void aspectAround(ProceedingJoinPoint pj){ System.out.println("IKAspect.aspectAroud,its 环绕通知前"); try { Object proceed = pj.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } System.out.println("IKAspect.aspectAroud,its 环绕通知后"); } } - 方式二
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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" > <bean id="ikAspect" class="com.jing.spring.aop.IKAspect"></bean> <bean id="ikAspectBiz" class="com.jing.spring.aop.IKAspectBiz"></bean> <aop:config> <aop:aspect id="ikAspectAop" ref="ikAspect"> <aop:around method="aspectAround" pointcut="execution(* com.jing.spring.aop.IKAspectBiz.*(..))"></aop:around> </aop:aspect> </aop:config> </beans>
package com.jing.spring.aop; import org.aspectj.lang.ProceedingJoinPoint; /**
*切面类
*/ public class IKAspect { public void aspectAround(ProceedingJoinPoint pj){ System.out.println("IKAspect.aspectAroud,its 环绕通知前"); try { Object proceed = pj.proceed(); } catch (Throwable throwable) { throwable.printStackTrace(); } System.out.println("IKAspect.aspectAroud,its 环绕通知后"); } } - Next
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」