Spring(十一)AspectJ框架开发AOP(基于xml)
来源:互联网 发布:可延迟服务器调度算法 编辑:程序博客网 时间:2024/06/05 17:43
说明
AspectJ是一个基于Java语言的AOP框架
Spring2.0以后新增了对AspectJ切点表达式支持
@AspectJ 是AspectJ1.5新增功能,通过JDK5注解技术,允许直接在Bean类中定义切面,所以可以使用xml方式和注解方式来开发AOP
新版本Spring框架,建议使用AspectJ方式来开发AOP
aspectj有五种通知
before( Formals )
前置通知(在方法执行前执行,如果通知抛出异常,阻止方法运行)
after( Formals ) returning [ ( Formal ) ]
后置通知(方法正常返回后执行,如果方法中抛出异常,通知无法执行
必须在方法执行后才执行,所以可以获得方法的返回值。)
after( Formals ) throwing [ ( Formal ) ]
异常停止(方法抛出异常后执行,如果方法没有抛出异常,无法执行)
after( Formals )
最终通知(方法执行完毕后执行,无论方法中是否出现异常,类似try-catch-finally里面的finally块)
around( Formals )
环绕通知( 方法执行前后分别执行,可以阻止方法的执行,必须手动执行目标方法)
在xml中对应:
导入的jar包:
1.aop联盟规范
2.spring aop实现
3.aspect规范
4.spring aspect实现
xml小例子
编写过程:
1.目标类:实现+接口
2.切面类:编写多个通知
3.aop编程,将通知应用到目标类
4.测试
目标类:
接口
public interface UserService { public boolean addUser(); public void updateUser(); public void deleteUser();}
实现
public class UserServiceImpl implements UserService { @Override public boolean addUser() { System.out.println("UserServiceDaoImpl addUser"); return true; } @Override public void updateUser() { //测试抛出异常通知 // int i=1/0; System.out.println("UserServiceDaoImpl updateUser"); } @Override public void deleteUser() { System.out.println("UserServiceDaoImpl deleteUser"); }}
切面类
五种通知对方法的方法名称没有限制,但是方法的格式有限制。方法的格式会在xml配置中给出。
public class MyAspect { //前置通知 public void before(JoinPoint joinPoint){ System.out.println("MyAspect-before"); } //最终通知 public void after(JoinPoint joinPoint){ System.out.println("MyAspect-after"); } //环绕通知 public Object around(ProceedingJoinPoint joinPoint) throws Throwable{ System.out.println("MyAspect-around-前"); Object obj=joinPoint.proceed();//执行目标方法 System.out.println("MyAspect-around-后"); return obj; } //后置通知 public void afterReturning(JoinPoint joinPoint,Object ret){ System.out.println("MyAspect-afterReturning "+joinPoint.getSignature().getName()+"\t"+ret); } //异常通知 public void afterThrowing(JoinPoint joinPoint,Throwable e){ System.out.println("MyAspect-afterThrowing "+e.getMessage()); }}
spring配置
<?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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 创建目标类 --> <bean id="userServiceId" class="com.scx.xmlproxy.test.UserServiceImpl"></bean> <!-- 创建切面类(通知) --> <bean id="myAspectId" class="com.scx.xmlproxy.test.MyAspect"></bean> <!-- aop编程--> <aop:config proxy-target-class="true"> <aop:aspect ref="myAspectId"> <aop:pointcut expression="execution(* com.scx.xmlproxy.test.*.*(..))" id="myPointCut"/> <!-- 前置通知 <aop:before method="before" pointcut-ref="myPointCut"/> 方法格式(参数1) 参数1:连接点描述 method:方法名 pointcut:切入点表达式 pointcut-ref:切入点引用 --> <!-- 最终通知 <aop:after method="after" pointcut-ref="myPointCut"/> 方法格式(参数1) 参数1:连接点描述 --> <!-- 环绕通知 <aop:around method="around" pointcut-ref="myPointCut"/> 方法格式(参数1) 参数:org.aspectj.lang.ProceedingJoinPoint --> <!-- 后置通知 <aop:after-returning method="afterReturning" returning="ret" pointcut-ref="myPointCut"/> 方法格式(参数1,参数2) 参数1:连接点描述 参数2:类型Object,参数名 returning="ret" 配置的 --> <!-- 抛出异常 <aop:after-throwing method="afterThrowing" pointcut-ref="myPointCut" throwing="e"/> 方法格式(参数1,参数2) 参数1:连接点描述对象 参数2:获得异常信息,类型Throwable ,参数名由throwing="e" 配置 --> </aop:aspect> </aop:config></beans>
测试:
测试时,最好只执行一个通知,否则结果会和想象的不一样orz.
@org.junit.Test public void testProxy(){ String xmlPath="com/scx/xmlproxy/test/applicationContext.xml"; ApplicationContext applicationContext=new ClassPathXmlApplicationContext(xmlPath); UserService userService=(UserService) applicationContext.getBean("userServiceId"); userService.addUser(); userService.updateUser(); userService.deleteUser(); }
测试结果:
前置通知:
后置通知:
异常通知:
为了出现异常我在实现类的updateUser方法里面里面添加了int i = 1 /0;
这行代码
结果如图所示输出了除0的异常
这时候我们修改为最终通知。运行结果:
我们发现updateUser方法没有因为异常输出,但是最终通知输出了。
环绕通知:
注解小例子在写篇文章给出~
- Spring(十一)AspectJ框架开发AOP(基于xml)
- Spring AOP技术(基于AspectJ)的XML开发
- Spring(十二)AspectJ框架开发AOP(基于注解)
- spring AOP切面开发 基于aspectJ框架切点开发
- Spring AOP基于@AspectJ开发
- 9、SSM框架-Spring AOP之基于XML的声明式AspectJ(9)
- [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.
- Spring AOP技术(基于AspectJ)的Annotation开发
- Spring的基于AspectJ的AOP开发
- Spring学习-22:Spring的AOP:基于AspectJ的XML配置方式开发
- SSM框架项目搭建系列(六)—Spring AOP之基于XML的声明式AspectJ
- spring AOP切面开发 基于aspectJ框架切点的注解开发
- Spring框架学习-深入理解AOP02----AOP简介,AspectJ,AOP基于注解和XML配置(5种通知,切面优先级)
- Spring AOP(二)(Spring中基于AOP的@AspectJ)
- Spring AOP使用Aspectj基于xml方式,初始化Bean参数
- Spring AOP编程(两种:1,传统的AOP切面编程 2,基于aspectj框架切点传统开发)
- 基于@AspectJ配置Spring AOP
- spring aop(四)--基于@AspectJ
- 正则表达式必知必会(一)
- Android中使用开源框架android-image-indicator实现图片轮播部署
- 对ArrayList进行分页
- 2004 饶毅,鲁白, 邹承鲁:中国科技需要的根本转变
- 数据结构一绪论(上)之 b计算模型
- Spring(十一)AspectJ框架开发AOP(基于xml)
- ORACLE中null比较运算
- 知识图谱学习之存储与查询(一 )
- select2插件实现列表的模糊搜索
- java程序员的基本修养(一)----数组其内存管理
- Variable references empty selection: ${project_loc}的解决办法
- 从零开始学习Android 开发笔记-Activity生命周期
- flume之hdfsSink分析
- 菜鸟小屁的成长日记2017.1.10