ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

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

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
返回列表