Spring、XML配置AOP
2016-12-01 00:00
239 查看
新建一个AOP类:
在beans.xml中注入,并配置AOP:
public class MyInterceptor2 { public void doAccessCheck(){ System.out.println("前置通知 "); } public void doAfterReturning(){ System.out.println("后置通知 "); } public void doAfter(){ System.out.println("最终通知"); } public void doAround(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("环绕通知前"); pjp.proceed(); System.out.println("环绕通知后"); } public void doAfterThrowing(Exception e){ System.out.println("例外通知 例外 e:"+e); } }
在beans.xml中注入,并配置AOP:
<aop:aspectj-autoproxy /> <bean id="personIService" class="cn.raffaello.service.impl.PersonServiceImpl"/> <bean id="myInterceptor2" class="cn.raffaello.aop.MyInterceptor2" /> <aop:config> <!-- 定义切面 --> <aop:aspect id="aspect" ref="myInterceptor2"> <!-- 定义切入点 --> <!-- 拦截所有返回值为String的方法:execution(java.lang.String cn.raffaello.service.impl.PersonServiceImpl.*(..)) --> <!-- 拦截所有返回值非void的方法:execution(!void cn.raffaello.service.impl.PersonServiceImpl.*(..)) --> <!-- 拦截第一个参数是String的方法:execution(* cn.raffaello.service.impl.PersonServiceImpl.*(java.lang.String,..)) --> <!-- 拦截包级子包下的所有类的所有的方法:execution(* cn.raffaello.service.*.*(..)) --> <!-- 拦截参数为String,并且参数名字为name: execution(* cn.raffaello.service.impl.PersonServiceImpl.*(String)) and args(name) --> <aop:pointcut id="pointcut" expression="execution(* cn.raffaello.service.impl.PersonServiceImpl.*(java.lang.String))" /> <!-- 前置通知 --> <aop:before pointcut-ref="pointcut" method="doAccessCheck"/> <!-- 后置通知 --> <!-- 拦截返回值为String的方法 <aop:after-returning pointcut-ref="pointcut" method="doAfterReturning" returning="retv"/> --> <aop:after-returning pointcut-ref="pointcut" method="doAfterReturning" /> <!-- 最终通知 --> <aop:after pointcut-ref="pointcut" method="doAfter" /> <!-- 环绕通知 --> <aop:around pointcut-ref="pointcut" method="doAround" /> <!-- 例外通知 --> <aop:after-throwing pointcut-ref="pointcut" method="doAfterThrowing" throwing="e"/> </aop:aspect> </aop:config>
相关文章推荐
- Spring配置文件applicationContext.xml(2)之AOP
- spring学习笔记8--使用spring进行面向切面的(AOP)编程(2)XML配置方式
- spring使用aop时xml中的配置
- (一)Spring AOP:基于XML配置文件
- spring context.xmL配置数据库事务以及aop
- applicationContext-common.xml 之spring tx:advice 和 aop:config 配置事务属性详解
- 8.4.6: Spring的AOP---基于XML配置文件的管理方式
- 关于SpringAOP的XML方式的配置
- Spring的AOP配置(基于xml)
- Spring学习一:IOC(控制反转)和AOP(面向切面)的xml配置和注解方式
- spring aop的使用(注解方式以及基于xml配置方式)
- Spring Aop实例之xml配置
- 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP
- Spring的AOP编程XML配置
- Spring Aop实现——Annotation方式(注解式)and Schema-based式(xml配置)
- spring aop的使用(注解方式以及基于xml配置方式)
- spring使用AOP如何配置ApplicationContext.xml
- Spring aop注解方式和xml配置方式
- springAOP基于xml的配置前后环绕通知
- Spring Aop实例之xml配置