Spring AOP(2) --基于配置文件方式配置
1、spring-aop-xml.xml
<?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-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!--配置切面bean--> <bean id="loggingAspectXml" class="com.spring.aop.xml.LoggingAspect"/> <bean id="validationAspectXml" class="com.spring.aop.xml.ValidationAspect"/> <!--配置bean--> <bean id="mathCalculatorXml" class="com.spring.aop.xml.MathCalculatorImpl"/> <!--*************aop配置*****************--> <!-- <aop:aspect/>和<aop:advisor/>的区别 < aop:aspect>:定义切面(切面包括通知和切点) < aop:advisor>:定义通知器(通知器跟切面一样,也包括通知和切点) 1、实现方式不同 < aop:aspect>定义切面时,只需要定义一般的bean就行, 而定义< aop:advisor>中引用的通知时,通知必须实现Advice接口。 2、使用场景不同 < aop:advisor>大多用于事务管理。 总结:< aop:advisor>和< aop:aspect>其实都是将通知和切面进行了封装,原理基本上是一样的,只是使用的方式不同而已。 --> <!--配置aop--> <aop:config> <!--配置切点表达式--> <aop:pointcut id="pointCut" expression="execution(* com.spring.aop.xml.MathCalculator.*(..))"/> <!--配置切面及通知--> <aop:aspect ref="loggingAspectXml" order="2"> <!-- <aop:before method="beforeMethod" pointcut-ref="pointCut"/> <aop:after method="afterMethod" pointcut-ref="pointCut"/> <aop:after-returning method="afterReturning" pointcut-ref="pointCut" returning="result"/> <aop:after-throwing method="afterThrowing" pointcut-ref="pointCut" throwing="e"/>--> <aop:around method="aroundMethod" pointcut-ref="pointCut"/> </aop:aspect> <!--测试优先级,order--> <aop:aspect ref="validationAspectXml" order="1"> <aop:before method="validateArgs" pointcut-ref="pointCut"/> </aop:aspect> </aop:config> </beans>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix