AOP-Spring的面向切面编程
2017-09-25 22:43
429 查看
开始开发之前,
需要导入jar包有:
aopalliance.jar、aspectjweaver-1.6.6.jar 、spring-aspects-4.0.6.RELEASE.jar
配置文件中beans头部标签要加入
xmlns:aop=”http://www.springframework.org/schema/aop”
其xsi:schemaLocation要加入
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd”
之后就是对切面类等的配置
切面相关配置
在切面类serviceAspect里面,比如
写onBefore方法
onAfter方法类似
环绕通知
其优先级在before后在after前
返回通知
异常通知
这个在beans.xml文件中的配置除了pointcut-ref 和 method,还要throwing
附加:
第一个*代表所有的返回值类型
第二个*代表所有的类
第三个*代表类所有方法
最后一个..代表所有的参数。
比如:
任何返回值、com.example.service包中任何类中的任何方法、任何参数
execution(* com.example.service..(..))
任何返回值、com.example.service包中任何层次子包(..)、任何类、任何方法、任何参数
execution(* com.example.service...(..))
void 和 !void(非void)
execution(public void com.example.service...(..))
execution(public !void com.example.service...(..))
需要导入jar包有:
aopalliance.jar、aspectjweaver-1.6.6.jar 、spring-aspects-4.0.6.RELEASE.jar
配置文件中beans头部标签要加入
xmlns:aop=”http://www.springframework.org/schema/aop”
其xsi:schemaLocation要加入
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd”
之后就是对切面类等的配置
<bean id = "serviceAspect" class = "com.example.aspect.ServiceAspect">
切面相关配置
<aop:config> // 此处定义的pointcut是全局的pointcut,可以供所有的aspect使用 // id:表示这个pointcut的名称 <aop:pointcut id="methodToBeAspected" expression="execution(* com.example.service..*.*(..))" /> // <aop:aspect>为定义一个切面类;id是切面类的名称;ref是引用哪个切面bean <aop:aspect id="随意的name都可行" ref="serviceAspect"> //在此处定义的pointcut只是在当前的aspect使用;id表示这个pointcut的名称,以方便使用 <aop:pointcut id="methodToBeAspected2" expression="execution(* com.example.service..*.*(..))" /> //pointcut-ref是指哪个方法作为切点;method是指在切面类中的方法 before是指在切点方法methodToBeAspected2执行之前执行切面类的onBefore方法。 <aop:before method="onBefore" pointcut-ref="methodToBeAspected2"/> <aop:after-returning method="onAfterReturning" pointcut="execution(* com.example.service..*.*(..))"/> </aop:aspect> </aop:config>
在切面类serviceAspect里面,比如
写onBefore方法
public void 4000 onBefore(JoinPoint jp) { jp.getTarget().getClass().getName();//可以获取切点类名 jp.getSignature().getName()//可以获取切点的执行方法名 jp.getArgs()[0]获取传入参数值,数组类型 }
onAfter方法类似
public void onAfter(JoinPoint jp) { jp.getTarget().getClass().getName();//可以获取切点类名 jp.getSignature().getName()//可以获取切点的执行方法名 jp.getArgs()[0]获取传入参数值,数组类型 }
环绕通知
public Object onAround(ProceedingJoinPoint pjp) throws Throwable { //前 Object obj = pjp.proceed() //有返回值,切点方法的返回值,此处就是切点方法执行了 ... //代码执行,自己写的监听代码 //后 return obj; }
其优先级在before后在after前
返回通知
public void onAfterReturning(JoinPoint jp) { jp.getTarget().getClass().getName();//可以获取切点类名 jp.getSignature().getName()//可以获取切点的执行方法名 jp.getArgs()[0]获取传入参数值,数组类型 }//像在环绕通知中切点方法执行后的返回值,一执行完就调用这个返回通知。
异常通知
public void onAfterThrowing(JoinPoint jp, Throwable ex ) { jp.getTarget().getClass().getName();//可以获取切点类名 jp.getSignature().getName()//可以获取切点的执行方法名 jp.getArgs()[0]获取传入参数值,数组类型 }
这个在beans.xml文件中的配置除了pointcut-ref 和 method,还要throwing
附加:
第一个*代表所有的返回值类型
第二个*代表所有的类
第三个*代表类所有方法
最后一个..代表所有的参数。
比如:
任何返回值、com.example.service包中任何类中的任何方法、任何参数
execution(* com.example.service..(..))
任何返回值、com.example.service包中任何层次子包(..)、任何类、任何方法、任何参数
execution(* com.example.service...(..))
void 和 !void(非void)
execution(public void com.example.service...(..))
execution(public !void com.example.service...(..))
相关文章推荐
- SpringBoot中使用AOP面向切面编程
- 深入理解面向切面的编程AOP、AspectJ、Spring
- [Spring]面向切面编程AOP【学习笔记】
- Spring面向切面编程AOP
- Spring AOP面向切面编程
- 使用Spring进行面向切面(AOP)编程
- Spring面向切面编程——Spring实现AOP方式——通过注解实现
- Spring之面向切面编程AOP(三)
- springAOP面向切面编程之日志记录功能
- Spring--AOP--面向切面编程
- Spring之AOP,面向切面编程
- Spring笔记——使用Spring进行面向切面(AOP)编程
- spring的AOP--面向切面编程
- 笔记——spring基本使用(控制反转IOC、面向切面编程AOP、声明式事务)
- Spring---AOP面向切面编程
- SpringBoot 面向切面编程AOP
- Spring in Action 入门之面向切面编程AOP
- 【spring-boot】spring aop 面向切面编程初接触--切点表达式
- Spring AOP:面向切面编程简介
- spring中面向切面编程AOP的简单应用