Spring學習筆記:使用代理實現AOP

AOP基礎知識

Spring AOP 即 Aspect-oriented programming,面向切面編程,是作爲面向對象編程的一種補充,專門用於處理系統中分佈於各個模塊(不同方法)中的交叉關注點的問題。簡單地說,就是一個攔截器( interceptor )攔截一些處理過程。

例如,當一 個method 被執行,Spring AOP 能夠劫持正在運行的 method ,在 method 執行前或者後加入一些額外的功能。

在 Spring AOP 中,支持 4 種類型的通知( Advice ):

  • Before advice - method 執行前通知
  • After returning advice - method 返回一個結果後通知
  • After throwing advice - method 拋出異常後通知
  • Around advice - 環繞通知,結合了以上三種

四種類型的通知詳解(Advice)

Before Advice

1.實現MethodBeforeAdvice接口,覆蓋before方法

import  java.lang.reflect.Method;
import com.springframework.aop.MethodBeforeAdvice;
public class AdviceBeforeMethod implements  MethodBeforeAdvice{
    public void before(Method arg0,Object[] args,Object target)
                                            throws Throwable{
        System.out.println("add some thing before the method");
    }
}

2.在xml文件中配置Advice的Bean

<bean id="Advice的Bean的Id" class="類名"></bean>

3.在xml文件中配置(proxy)的Bean

<bean id="生成的代理的Bean的Id" class="org.spring.aop.framework.ProxyFactoryBean">
    <property name="target" ref="要攔截的類的Bean的Id"/>
    <property name="interceptorNames">
        <list>
            <value>使用的Advice的Bean的Id</value>
        </list>
    </property>
</bean>

After Returning Advice

1.實現AfterReturningAdvice 接口,覆蓋afterReturning方法

import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterMethodAdvice implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("add some thing after returning");
    }
}

2.在xml文件中配置Advice的Bean

<bean id="Advice的Bean的Id" class="類名"></bean>

3.在xml文件中配置(proxy)的Bean

<bean id="生成的代理的Bean的Id" class="org.spring.aop.framework.ProxyFactoryBean">
    <property name="target" ref="要攔截的類的Bean的Id"/>
    <property name="interceptorNames">
        <list>
            <value>使用的Advice的Bean的Id</value>
        </list>
    </property>
</bean>

After Returning Advice

1.實現AfterReturningAdvice 接口,覆蓋afterReturning方法

import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterMethodAdvice implements AfterReturningAdvice {
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("add some thing after returning");
    }
}

2.在xml文件中配置Advice的Bean

<bean id="Advice的Bean的Id" class="類名"></bean>

3.在xml文件中配置(proxy)的Bean

<bean id="生成的代理的Bean的Id" class="org.spring.aop.framework.ProxyFactoryBean">
    <property name="target" ref="要攔截的類的Bean的Id"/>
    <property name="interceptorNames">
        <list>
            <value>使用的Advice的Bean的Id</value>
        </list>
    </property>
</bean>

After Throwing Advice

1.實現ThrowsAdvice接口,覆蓋afterThrowing方法

import org.springframework.aop.ThrowsAdvice;
public class AfterThrowingAdvice implements ThrowsAdvice {
           public void afterThrowing(IllegalArgumentException e)throws Throwable{
               System.out.println("add some thing after throw exception");
           }
 }

2.在xml文件中配置Advice的Bean

<bean id="Advice的Bean的Id" class="類名"></bean>

3.在xml文件中配置(proxy)的Bean

<bean id="生成的代理的Bean的Id" class="org.spring.aop.framework.ProxyFactoryBean">
    <property name="target" ref="要攔截的類的Bean的Id"/>
    <property name="interceptorNames">
        <list>
            <value>使用的Advice的Bean的Id</value>
        </list>
    </property>
</bean>

Around Advice

1.實現接口MethodInterceptor,通過methodInvocation.proceed()來調用原方法

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import java.util.Arrays;

public class AroundAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        //取出方法
        System.out.println("Method name : " + methodInvocation.getMethod().getName());
        //取出方法的參數
        System.out.println("Method arguments : " + Arrays.toString(methodInvocation.getArguments()));
        //相當於MethodBeforeAdvice
        System.out.println("add some thing before the method");
        try{
            //調用原方法
            Object result = methodInvocation.proceed();
            //相當於AfterReturningAdvice
            System.out.println("add some thing after the method");
            return result;
        }catch (IllegalArgumentException e){
            //相當於ThrowsAdvice
            System.out.println();
            throw e;
        }
    }
}

2.在xml文件中配置Advice的Bean

<bean id="Advice的Bean的Id" class="類名"></bean>

3.在xml文件中配置(proxy)的Bean

<bean id="生成的代理的Bean的Id" class="org.spring.aop.framework.ProxyFactoryBean">
    <property name="target" ref="要攔截的類的Bean的Id"/>
    <property name="interceptorNames">
        <list>
            <value>使用的Advice的Bean的Id</value>
        </list>
    </property>
</bean>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章