浅谈spring——增强接口(六)
AOP支持5种类型的增强。
增强接口关系图:

1)前置增强:BeforeAdvice,表示在目标方法执行前实施增强。
2)后置增强:AfterReturningAdvice,表示在目标方法执行后实施增强。
3)环绕增强:MethodInterceptor,表示在目标方法执行前后实施增强
4)异常增强:ThrowsAdvice,表示在目标方法抛出异常后实施增强
5)引介增强:IntroductionInterceptor,表示在目标类中添加一些新的属性和方法
xml配置:
<bean id="greetingBefore" class="com.baobaotao.advice.GreetingBeforeAdvice" /><bean id="target" class="com.baobaotao.advice.NaiveWaiter" /><bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean"p:proxyInterfaces="com.baobaotao.advice.Waiter" ##代理接口p:target-ref="target" ##指定对哪个bean增强p:interceptorNames="greetingBefore"/> ##指定的增强
1)前置增强:
import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class GreetingBeforeAdvice implements MethodBeforeAdvice {public void before(Method method, Object[] args, Object obj) throws Throwable {String clientName = (String)args[0];System.out.println("How are you!Mr."+clientName+".");}}method:目标类的方法
args:目标类方法的入参
obj:目标类实例
import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;public class GreetingAfterAdvice implements AfterReturningAdvice {public void afterReturning(Object returnObj, Method method, Object[] args,Object obj) throws Throwable {System.out.println("Please enjoy yourself!");}}returnObj:目标实例方法返回的结果method:目标类方法
args:目标实例方法的入参
obj:目标类实例
3)环绕增强
import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class GreetingInterceptor implements MethodInterceptor {public Object invoke(MethodInvocation invocation) throws Throwable {Object[] args = invocation.getArguments();//获取入参String clientName = (String)args[0];System.out.println("How are you!Mr."+clientName+".");Object obj = invocation.proceed();//反射机制调用目标方法System.out.println("Please enjoy yourself!");return obj;}}MethodInvocation不但封装了目标方法及其入参数组,还封装了目标方法所在的实例对象4)抛出异常的增强
import java.lang.reflect.Method;import org.springframework.aop.ThrowsAdvice;public class TransactionManager implements ThrowsAdvice {public void afterThrowing(Method method, Object[] args, Object target,Exception ex) throws Throwable {System.out.println("-----------");System.out.println("method:" + method.getName());System.out.println("抛出异常:" + ex.getMessage());System.out.println("成功回滚事务。");}}ThrowAdvice异常抛出增强接口没有定义任何方法,它是一个标识接口,在运行时由spring使用反射机制自行判断。method:目标类方法
args:目标实例方法的入参
target:目标类实例
ex:要捕获的异常
其中前三个参数可以缺省。afterThrowing方法,可以定义多个,当目标方法抛出异常时,spring会自动找出最匹配的增强方法。
5)引介增强:比较特殊,不是在目标方法周围织入增强,而是为目标类增加新的方法和属性,属于类级别而非方法级别。
接口