1.依赖包

2.文件结构

3.接口类ISomeService

package com.buckwheats.test;public interface ISomeService {public void doFirst(String user);public String doSecond(String user);public void doThird();
}

4.ISomeService的实现类SomeService

package com.buckwheats.test;public class SomeService implements ISomeService {@Overridepublic void doFirst(String user) {System.out.println("执行doFirst");}@Overridepublic String doSecond(String user) {System.out.println("执行doSecond");return "buckwheat";}@Overridepublic void doThird() {System.out.println("执行doThird" +3/0);}
}

5.切入类MySystem

package com.buckwheats.test;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import java.util.Arrays;
public class MySystem {//前置方法,指定doFirst为切入点public void befor(){System.out.println("执行前置方法");}public void befor(JoinPoint jp){System.out.println("执行前置方法 参数user:"+Arrays.toString(jp.getArgs()));}//后置方法,无参数public void AfterReturning(){System.out.println("执行后置方法 不带参数");}//后置方法,有参数public void AfterReturning(JoinPoint jp){//输出传过来的参数//直接输出jp.getArgs()会得到一个数组对象,但是并不知道里面的内容,需要调用Arrays的toString()方法System.out.println("执行后置方法 带参数 args:"+ Arrays.toString(jp.getArgs()));}//环绕通知,可以改变目标方法的返回参数public String myAround(ProceedingJoinPoint result) throws Throwable {System.out.println("执行环绕通知,前置方法");Object obj = result.proceed();//获取传过来的参数//获取直接输出jp.getArgs()会得到一个数组对象,但是并不知道里面的内容,需要调用Arrays的toString()方法System.out.println("执行环绕通知 获取参数 args:"+ Arrays.toString(result.getArgs()));System.out.println("执行环绕通知,后置方法");return ((String)obj).toUpperCase();}//异常通知,当doThird出现异常时执行public void myAfterThrowing(Exception ex){System.out.println("出现异常 e:"+ex);}//"最终通知,不管是否出现异常,这个方法都会执行,且在最后执行public void myAfter(){System.out.println("最后通知,不管是否出现异常,这个方法都会执行");}//最终通知,有参数public void myAfter(JoinPoint jp){System.out.println("执行最终方法 ,带参数,切入点(方法):"+jp.getSignature().getName());}
}

6.配置文件applicationContext.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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!--注册目标方法--><bean id="someService" class="com.buckwheats.test.SomeService"/><!--注册切面--><bean id="myAdvice" class="com.buckwheats.test.MySystem"/><aop:config ><aop:aspect ref="myAdvice"><aop:pointcut id="doFirst" expression="execution(* *..ISomeService.doFirst(..))"/><aop:pointcut id="doSecond" expression="execution(* *..ISomeService.doSecond(..))"/><aop:pointcut id="doThird" expression="execution(* *..ISomeService.doThird(..))"/><aop:before method="befor" pointcut-ref="doFirst"/><!--前置方法,不带参数--><aop:before method="befor(org.aspectj.lang.JoinPoint)"  pointcut-ref="doFirst"/><!--前置方法,带参数--><aop:after-returning method="AfterReturning" pointcut-ref="doSecond" /> <!--后置方法,不带参数--><aop:after-returning method="AfterReturning(org.aspectj.lang.JoinPoint)" pointcut-ref="doSecond" /><aop:around method="myAround" pointcut-ref="doSecond" /> <!--环绕通知--><aop:after-throwing method="myAfterThrowing" pointcut-ref="doThird" throwing="ex"/> <!--执行异常通知--><aop:after method="myAfter" pointcut-ref="doThird" /><!--执行最终方法,不管是否出现在异常都会执行--><aop:after method="myAfter(org.aspectj.lang.JoinPoint)" pointcut-ref="doFirst" /></aop:aspect></aop:config>
</beans>

7.测试类Test

package com.buckwheats.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {@org.junit.Testpublic void test01(){ApplicationContext ac = new ClassPathXmlApplicationContext("com/buckwheats/test/applicationContext.xml");ISomeService service = (ISomeService) ac.getBean("someService");service.doFirst("oooo");System.out.println("=========");System.out.println(service.doSecond("iii"));System.out.println("==============");service.doThird();}
}

8.输出结果

Java spring基于XML的aop配置实现相关推荐

  1. spring中基于XML的AOP配置步骤

    spring中基于XML的AOP配置步骤 IAccountService.java package com.itheima.service;/*** 账户的业务层接口*/ public interfa ...

  2. 基于XML的AOP配置

    创建spring的配置文件并导入约束 此处要导入aop的约束 <?xml version="1.0" encoding="UTF-8"?> < ...

  3. 4、基于XML的AOP配置

    一.面向切面编程(AOP) 1.AOP概述 1.面向切面编程(Aspect Oriented Programming,简称AOP)通过提供另一种思考程序结构的方式来补充面向对象编程 (Object O ...

  4. Spring基于注解的AOP配置

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  5. spring基于aspectj的AOP配置 aop:aspectj-autoproxy proxy-target-class=true

    精通Spring4.x--企业应用开发实战 8.5.1@AfterReturning("@annotation()")切点函数详解 代码实现的目标是为NaugthyWaiter类的 ...

  6. 基于xml的方式配置AOP

    用基于 XML 的配置声明切面 除了使用 AspectJ 注解声明切面, Spring 也支持在 Bean 配置文件中声明切面. 这种声明是通过 aop schema 中的 XML 元素完成的. 正常 ...

  7. Spring框架----Spring的基于XML的AOP的实现

    导入依赖 <dependency><groupId>org.springframework</groupId><artifactId>spring-co ...

  8. spring基于XML的AOP-编写必要的代码

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  9. spring 基于XML的申明式AspectJ通知的执行顺序

    spring 基于XML的申明式AspectJ通知的执行顺序 关于各种通知的执行顺序,结论:与配置文件中的申明顺序有关 1. XML文件配置说明 图片来源:<Java EE企业级应用开发教程&g ...

最新文章

  1. 字节「大力」做教育,依然技术思维,1万员工参与,三年不考虑盈利
  2. 1.Hadoop初步环境搭建(ssh无密码通信)
  3. php数组根据指定列排序
  4. 安卓重构系列-01使用Kotlin开发第一个Demo
  5. [HNOI2009]有趣的数列
  6. Akka之actor模型
  7. 测试技术培训:如何测试磁盘写的速度
  8. html5-table布局
  9. django相关操作
  10. java使用elasticsearch进行模糊查询-已在项目中实际应用
  11. EXCEL闪退的处理方法
  12. 机器学习高频面试题(41道)
  13. C++实现截图截屏功能汇总
  14. 百兆和千兆直通线与交叉线的制作方法
  15. ZZULIOJ1076-1080Python解法
  16. PS_变量批量生产名片
  17. DualVD:借鉴认知双向编码理论,提出视觉对话新框架 | AAAI 2020
  18. 锐龙r5 5600h核显什么水平 r5 5600h属于什么级别
  19. 最全的盲埋孔板工艺介绍与设计原则​​​​​​​
  20. 基于cnn的图像二分类算法(一)

热门文章

  1. c语言求幸运数字程序,算法题挑选幸运数字,该如何处理
  2. vim实用指南(一):光标移动(3)——滚动屏幕
  3. ubuntu16.04挂载三星T5移动硬盘报错
  4. 开源硬件论坛,燃烧你的创造力 | COSCon'18 特辑
  5. 电力电子技术填空题(80+道),适合期末复习、面试等
  6. 案例研究丨运动品牌On昂跑如何通过DTC创新实现全球化战略
  7. 计算机网络 UDP协议与TCP协议首部
  8. word文档表格中插入图片设置
  9. MM的爆强语录(转)
  10. 产业互联网将会取代消费互联网成为一个全新的风口