spring aop的两种实现方式(续1)
接上次。。。
第一种实现方式:针对于拦截多个包中的某一规则的方法
配置文件:
关键拦截类AopAdvice.java:
拦截类AopSingleAdvice.java实现package cn.icbc.service.bs.impl;import java.lang.reflect.Method;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;import org.springframework.aop.AfterReturningAdvice;import org.springframework.aop.MethodBeforeAdvice;public class AopSingleAdvice implements MethodBeforeAdvice, AfterReturningAdvice,MethodInterceptor{public void afterReturning(Object arg0, Method arg1, Object[] arg2,Object arg3) throws Throwable {System.out.println("spring aop afterReturning 权限拦截测试....");}public void before(Method arg0, Object[] arg1, Object arg2)throws Throwable {System.out.println("spring aop before 权限拦截测试....");}public Object invoke(MethodInvocation arg0) throws Throwable {System.out.println("spring aop arount before 权限拦截测试....");Object o=arg0.proceed();System.out.println("spring aop arount after 权限拦截测试....");return o;}}
补充说明:
1、两种实现方式的应用场景并不是绝对的,相互间可以通用,对于第二种方式中,可以引入org.springframework.aop.support.NameMatchMethodPointcutAdvisor和
org.springframework.aop.support.RegexpMethodPointcutAdvisor 来应用于跨包的拦截(待下次说明具体配置和实现)
2、例子只是列举了before,after,around的通知方式,其它的于此相似,可以自行举一反三.
3、此两种方式是在spring2.0的基础上实现的
4、第二种方式下,获得bean的名字应该是addProxy。