一、前置增強
二、后置增強
三、環繞增強
環繞增強相當於前置增強和后置增強的結合體,可以使用<aop:around>進行處理,這里我采用代理工廠的方式
1.接口及其實現類
public interface ProService { public void doSome(); }
public class ProServiceImpl implements ProService { @Override public void doSome() { System.out.println("123"); } }
2.增強類
public class around implements MethodInterceptor { @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("before"); Object proceed = methodInvocation.proceed(); System.out.println("after"); return proceed; } }
3.配置文件
<!--注入bean--> <bean id="proService" class="cn.spring.around.ProServiceImpl"></bean> <!--切面--> <bean id="aroundAdvice" class="cn.spring.around.around"></bean> <!--代理工廠實現增強--> <bean id="ProFactory" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--將增強和業務織入到一起--> <property name="target" ref="proService"></property> <!--攔截增強類--> <property name="interceptorNames" value="aroundAdvice"></property> <!--更換代理方式 默認值為false jdk動態代理,當沒有接口時,自動改成cglib--> <property name="proxyTargetClass" value="true"></property> </bean>
或者使用aop:config
<!--將目標對象聲明到Spring容器中--> <bean id="doSomeService" class="com.cmy.service.impl.DoSomeServiceImpl"></bean> <!-- 聲明增強方法所在的Bean --> <bean id="advice" class="com.cmy.around.AroundLogger"></bean> <!-- 配置切面 --> <aop:config> <aop:aspect ref="advice"> <aop:around method="aroundLogger" pointcut="execution(* com.cmy.*.*.*(..))"/> </aop:aspect> </aop:config>
四、異常增強
異常增強處理,在目標方法拋出異常后織入;使用<aop:after-throwing>處理,這里我依舊采用代理工廠的方法
1.接口及其實現類
public interface IdoSomeService { public void doSome() throws Exception; }
/** * 原始對象 */ public class IdoSomeServiceImpl implements IdoSomeService { public void doSome() throws Exception{ int result=5/0; System.out.println("=========真實業務==========="); } }
2.增強類
package cn.spring.throwadvice; import org.springframework.aop.AfterAdvice; import org.springframework.aop.AfterReturningAdvice; import org.springframework.aop.ThrowsAdvice; import java.lang.reflect.Method; public class MyAdvice { public void afterThrowing(Exception ex){ System.out.println("=====發生了異常,執行增強操作==============="); } }
3.配置文件
<bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 將增強和業務織入到一起--> <property name="target" ref="idoSomeService"></property> <!--攔截增強類;--> <property name="interceptorNames" value="myThrowAdvice"></property> <!-- 更換代理方式 proxyTargetClass默認值為false 默認是jdk動態代理,但是當目標對象沒有接口時,自動改為cglib --> <property name="proxyTargetClass" value="true"></property> </bean>
或者采用aop:after-throwing
<aop:config> <aop:pointcut id="pointcut" expression="execution(* *..throwadvice.*.*(..))"/> <aop:aspect ref="myAdvice"> <aop:after-throwing method="afterThrowing" throwing="ex" pointcut-ref="pointcut"></aop:after-throwing> </aop:config>
五、最終增強
無論方法是否拋出異常,都會在目標方法后做織入的增強處理,即該增強一定會執行,有點類似try-catch-finally塊中的finally,一般用於釋放資源。 使用<aop:after>處理最終增強。這里依舊運用代理工廠實現
1.增強類
public void afterAdvice(){ System.out.println("======執行最終異常==============="); }
2.配置文件
<aop:config> <aop:pointcut id="pointcut" expression="execution(* *..throwadvice.*.*(..))"/> <aop:aspect ref="myAdvice"> <aop:after method="afterAdvice" pointcut-ref="pointcut"></aop:after> </aop:aspect> </aop:config>