玉溪市网站建设_网站建设公司_门户网站_seo优化
2025/12/28 14:45:23 网站建设 项目流程

第一步:编写目标类

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();}}

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询