2025年必看:GEO排名服务商口碑与实力双榜发布,ai搜索排名/GEO排名/GEO/矩阵/ai数字人排行榜GEO排名老牌公司排行 - 品牌推荐师
2025/12/28 17:21:08
第一步:编写目标类
packagecom.powernode.spring6.service;// 目标类publicclassVipService{publicvoidadd(){System.out.println("保存vip信息。");}}第二步:编写切面类,并且编写通知
packagecom.powernode.spring6.service;importorg.aspectj.lang.ProceedingJoinPoint;// 负责计时的切面类publicclassTimerAspect{publicvoidtime(ProceedingJoinPointproceedingJoinPoint)throwsThrowable{longbegin=System.currentTimeMillis();//执行目标proceedingJoinPoint.proceed();longend=System.currentTimeMillis();System.out.println("耗时"+(end-begin)+"毫秒");}}第三步:编写spring配置文件
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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"><!--纳入spring bean管理--><beanid="vipService"class="com.powernode.spring6.service.VipService"/><beanid="timerAspect"class="com.powernode.spring6.service.TimerAspect"/><!--aop配置--><aop:config><!--切点表达式--><aop:pointcutid="p"expression="execution(* com.powernode.spring6.service.VipService.*(..))"/><!--切面--><aop:aspectref="timerAspect"><!--切面=通知 + 切点--><aop:aroundmethod="time"pointcut-ref="p"/></aop:aspect></aop:config></beans>测试程序:
packagecom.powernode.spring6.test;importcom.powernode.spring6.service.VipService;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassAOPTest3{@TestpublicvoidtestAOPXml(){ApplicationContextapplicationContext=newClassPathXmlApplicationContext("spring-aop-xml.xml");VipServicevipService=applicationContext.getBean("vipService",VipService.class);vipService.add();}}