新闻详情

新闻详情

首页 / 资讯中心 / 详情

Spring AOP—基于XML的AOP实现(IDEA2026+JDK17)

发布时间:2026/9/25 17:57:28来源:尧图网络
Spring AOP—基于XML的AOP实现(IDEA2026+JDK17)
0.环境IDEA2026.1JDK17spring: 5.3.201.创建项目打开IDEA点击文件—新建—项目然后下面选择”Java“名称为SpringAOPXml构建系统选MavenJDK版本选17。最后点击”创建“。创建完后项目基本结构如下然后我们给项目创建项目结构。右击main下面的java文件夹然后选择”新建“再点击”软件包“软件包命参考如下com.guo.aop可以根据实际情况修改包名。创建完后项目结构如下如果你是下面这种结构想展开的话那么继续往下看。在项目上面有三个点点击然后选择”外观“保证”压缩空的中间软件包“前面没有对号。2.引入依赖打开pom.xml文件引入下面的依赖文件把dependencies全部内容放进去?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.demo/groupIdartifactIdSpringAOPXml/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependencies!-- spring-core的依赖包 --dependencygroupIdorg.springframework/groupIdartifactIdspring-core/artifactIdversion5.3.20/version/dependency!-- spring-beans的依赖包 --dependencygroupIdorg.springframework/groupIdartifactIdspring-beans/artifactIdversion5.3.20/version/dependency!-- spring-context的依赖包 --dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.3.20/version/dependency!-- spring-expression的依赖包 --dependencygroupIdorg.springframework/groupIdartifactIdspring-expression/artifactIdversion5.3.20/version/dependency!-- commons-logging的依赖包 --dependencygroupIdcommons-logging/groupIdartifactIdcommons-logging/artifactIdversion1.2/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-aop/artifactIdversion5.3.20/version/dependency!-- aspectjrt包的依赖 --dependencygroupIdorg.aspectj/groupIdartifactIdaspectjrt/artifactIdversion1.9.7/version/dependency!-- aspectjweaver包的依赖 --dependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.9.7/version/dependency/dependencies/project一开始引入报错是正常的因为还没有下载下依赖我们有两种方式。方式1直接点击右上角的刷新。方法2先点击右侧的maven插件再点击上面的两个刷新。注意你先要配置正确maven环境这里才能正常下载下来的依赖。3.创建Dao文件xxxDao是接口文件也可以使用包来包裹这个文件右击aop文件夹选择”新建“然后选择”Java类“。这个地方的类型要选接口接口名为UserDao添加代码packagecom.guo.aop;publicinterfaceUserDao{publicvoidinsert();publicvoiddelete();publicvoidupdate();publicvoidselect();}添加完后项目如下4.创建Impl文件xxxImpl文件是xxxDao具体实现类这里面要写具体的业务代码。右击aop文件夹选择”新建“再选择“Java类”这里的选择的类型是:类类名为UserDaoImpl参考代码packagecom.guo.aop;publicclassUserDaoImplimplementsUserDao{publicvoidinsert(){System.out.println(添加用户信息);}publicvoiddelete(){System.out.println(删除用户信息);}publicvoidupdate(){System.out.println(更新用户信息);}publicvoidselect(){System.out.println(查询用户信息);}}注意这里使用implements 实现了UserDao的接口实现后的项目如下5.创建切面类这个就是需要切进去的代码。右击aop文件夹选择“新建”然后点击“Java类”切面类名为XmlAdvice参考代码packagecom.guo.aop;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.ProceedingJoinPoint;publicclassXmlAdvice{//前置通知publicvoidbefore(JoinPointjoinPoint){System.out.print(这是前置通知!);System.out.print(目标类是joinPoint.getTarget());System.out.println(被织入增强处理的目标方法为joinPoint.getSignature().getName());}//返回通知publicvoidafterReturning(JoinPointjoinPoint){System.out.print(这是返回通知方法不出现异常时调用!);System.out.println(被织入增强处理的目标方法为joinPoint.getSignature().getName());}/** * 环绕通知 * ProceedingJoinPoint 是JoinPoint子接口表示可以执行目标方法 * 1.必须是Object类型的返回值 * 2.必须接收一个参数类型为ProceedingJoinPoint * 3.必须throws Throwable */publicObjectaround(ProceedingJoinPointpoint)throwsThrowable{System.out.println(这是环绕通知之前的部分);//调用目标方法Objectobjectpoint.proceed();System.out.println(这是环绕通知之后的部分);returnobject;}//异常通知publicvoidafterException(){System.out.println(异常通知);}//后置通知publicvoidafter(){System.out.println(这是后置通知);}}创建后项目内容如下6.创建配置文件右击resourtces文件夹选择“新建”然后点击“文件”。文件名为applicationContext.xml?xml version1.0 encodingUTF-8?beansxmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd!-- 注册Bean --beannameuserDaoclasscom.guo.aop.UserDaoImpl/beannamexmlAdviceclasscom.guo.aop.XmlAdvice/!-- 配置Spring AOP--aop:config!-- 指定切点 --aop:pointcutidpointcutexpressionexecution(* com.guo.aop.UserDaoImpl.*(..))/!-- 指定切面 --aop:aspectrefxmlAdvice!-- 指定前置通知 --aop:beforemethodbeforepointcut-refpointcut/!-- 指定返回通知 --aop:after-returningmethodafterReturningpointcut-refpointcut/!-- 指定环绕方式 --aop:aroundmethodaroundpointcut-refpointcut/!-- 指定异常通知 --aop:after-throwingmethodafterExceptionpointcut-refpointcut/!-- 指定后置通知 --aop:aftermethodafterpointcut-refpointcut//aop:aspect/aop:config/beans创建后项目如下7.编写测试代码测试代码可以放到测试类中这里为了方便暂时放到main函数中我们创建main函数。右击aop点击“新建”在点击“Java类”类名为XmlTest参考代码packagecom.guo.aop;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassXmlTest{publicstaticvoidmain(String[]args){ApplicationContextcontextnewClassPathXmlApplicationContext(applicationContext.xml);UserDaouserDaocontext.getBean(userDao,UserDao.class);userDao.delete();System.out.println();userDao.insert();System.out.println();userDao.select();System.out.println();userDao.update();}}8.测试运行后可以看到切面类代码正常切入正常执行。本文源代码下载1.百度网盘通过网盘分享的文件SpringAOPXml.zip链接: https://pan.baidu.com/s/1Zkukv6eWID8RZtxsLS3BQg?pwdiiej 提取码: iiej2.夸克网盘我用夸克网盘给你分享了「SpringAOPXml.zip」点击链接或复制整段内容打开「夸克APP」即可获取。/0edc3b4gHe链接https://pan.quark.cn/s/daffd91621ea?pwdHX7y提取码HX7y
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

dsh-anchored-standard Wire级Think-Execute分离完全指南:如何在Adapter层用tool_choice:none实现工具“可见不可调用“ 2026/9/25 18:55:48

dsh-anchored-standard Wire级Think-Execute分离完全指南:如何在Adapter层用tool_choice:none实现工具“可见不可调用“

dsh-anchored-standard Wire级Think-Execute分离完全指南:如何在Adapter层用tool_choice:none实现工具"可见不可调用" 【免费下载链接】dsh-anchored-standard Two-phase DeepSeek Harness preset: Minimal-aligned bootstrap, then full Standard tools …

阅读更多 →
做了3个月AI旅行产品,最大挑战不是技术 2026/9/25 18:55:41

做了3个月AI旅行产品,最大挑战不是技术

项目第87天,我收到一封邮件,发件人是合规部门,标题里有暂停两个字。那时候我们的产品内测刚跑两周,技术坑全部填平,团队正在讨论上线排期。这封邮件一来,整条产品线原地停摆。这篇讲讲这三个月里发生的事&a…

阅读更多 →
腾讯 BrowserSkill 本地实测:CLI 装完不能用,真正的边界在 52800 端口 2026/9/25 18:55:28

腾讯 BrowserSkill 本地实测:CLI 装完不能用,真正的边界在 52800 端口

腾讯开源的 BrowserSkill 让 AI Agent 复用你已登录的真实浏览器,GitHub 上 5,718 star(9 月 20 日查的),适配 Cursor、Claude Code、Codex、OpenClaw、CodeBuddy、WorkBuddy、Hermes Agent、DeepSeek Harness 等一批主流 harness…

阅读更多 →
低预算也能用上的 TTS 语音合成工具盘点:2026 年平价配音方案怎么选? 2026/9/25 18:55:22

低预算也能用上的 TTS 语音合成工具盘点:2026 年平价配音方案怎么选?

摘要: 低预算做配音/语音合成,决策只看三件事——免费额度是否够用、商用授权是否清晰、音色自然度是否达标。本文用一张横向对比表,把 Edge TTS、逗哥配音、微软 Azure、百度/阿里/腾讯云语音合成等 10 款平价方案的参数摆清楚,并…

阅读更多 →
Atlas 300V 24G推理加速卡与YOLO部署实战全解析 2026/9/25 18:55:22

Atlas 300V 24G推理加速卡与YOLO部署实战全解析

最近总有人问我“Atlas 300V 24G是运算加速卡吗”,还有人问“网上那些用Atlas部署YOLO的教程,到底靠不靠谱”。作为一块用过挺久的推理卡,我觉得有必要把这块卡从硬件定位到软件部署的完整经验一次性讲清楚。Atlas 300V 24G是华为昇腾生态里一…

阅读更多 →
昇腾ATLAS 300V 24G部署YOLO实战:从推理卡选型到性能调优 2026/9/25 18:55:22

昇腾ATLAS 300V 24G部署YOLO实战:从推理卡选型到性能调优

2. 硬件认知:ATLAS 300V 24G到底是一张什么卡ATLAS 300V 24G是华为昇腾系列面向边缘推理场景推出的一款AI加速卡,核心芯片为昇腾310P系列处理器。很多人第一次拿到这张卡,会下意识地把它和GPU放在一起对比,比如“它是不是对标RTX …

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉