Java spring基于XML的aop配置实现
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配置实现相关推荐
- spring中基于XML的AOP配置步骤
spring中基于XML的AOP配置步骤 IAccountService.java package com.itheima.service;/*** 账户的业务层接口*/ public interfa ...
- 基于XML的AOP配置
创建spring的配置文件并导入约束 此处要导入aop的约束 <?xml version="1.0" encoding="UTF-8"?> < ...
- 4、基于XML的AOP配置
一.面向切面编程(AOP) 1.AOP概述 1.面向切面编程(Aspect Oriented Programming,简称AOP)通过提供另一种思考程序结构的方式来补充面向对象编程 (Object O ...
- Spring基于注解的AOP配置
pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- spring基于aspectj的AOP配置 aop:aspectj-autoproxy proxy-target-class=true
精通Spring4.x--企业应用开发实战 8.5.1@AfterReturning("@annotation()")切点函数详解 代码实现的目标是为NaugthyWaiter类的 ...
- 基于xml的方式配置AOP
用基于 XML 的配置声明切面 除了使用 AspectJ 注解声明切面, Spring 也支持在 Bean 配置文件中声明切面. 这种声明是通过 aop schema 中的 XML 元素完成的. 正常 ...
- Spring框架----Spring的基于XML的AOP的实现
导入依赖 <dependency><groupId>org.springframework</groupId><artifactId>spring-co ...
- spring基于XML的AOP-编写必要的代码
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- spring 基于XML的申明式AspectJ通知的执行顺序
spring 基于XML的申明式AspectJ通知的执行顺序 关于各种通知的执行顺序,结论:与配置文件中的申明顺序有关 1. XML文件配置说明 图片来源:<Java EE企业级应用开发教程&g ...
最新文章
- 字节「大力」做教育,依然技术思维,1万员工参与,三年不考虑盈利
- 1.Hadoop初步环境搭建(ssh无密码通信)
- php数组根据指定列排序
- 安卓重构系列-01使用Kotlin开发第一个Demo
- [HNOI2009]有趣的数列
- Akka之actor模型
- 测试技术培训:如何测试磁盘写的速度
- html5-table布局
- django相关操作
- java使用elasticsearch进行模糊查询-已在项目中实际应用
- EXCEL闪退的处理方法
- 机器学习高频面试题(41道)
- C++实现截图截屏功能汇总
- 百兆和千兆直通线与交叉线的制作方法
- ZZULIOJ1076-1080Python解法
- PS_变量批量生产名片
- DualVD:借鉴认知双向编码理论,提出视觉对话新框架 | AAAI 2020
- 锐龙r5 5600h核显什么水平 r5 5600h属于什么级别
- 最全的盲埋孔板工艺介绍与设计原则​​​​​​​
- 基于cnn的图像二分类算法(一)