溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring AOP的幾種實現方式總結

發(fā)布時間:2020-10-09 19:44:36 來源:腳本之家 閱讀:583 作者:Mercop 欄目:編程語言

AOP核心概念

1、橫切關注點

對哪些方法進行攔截,攔截后怎么處理,這些關注點稱之為橫切關注點

2、切面(aspect)

類是對物體特征的抽象,切面就是對橫切關注點的抽象

3、連接點(joinpoint)

被攔截到的點,因為spring只支持方法類型的連接點,所以在Spring中連接點指的就是被攔截到的方法,實際上連接點還可以是字段或者構造器

4、切入點(pointcut)

對連接點進行攔截的定義

5、通知(advice)

所謂通知指的就是指攔截到連接點之后要執(zhí)行的代碼,通知分為前置、后置、異常、最終、環(huán)繞通知五類

6、目標對象

代理的目標對象

7、織入(weave)

將切面應用到目標對象并導致代理對象創(chuàng)建的過程

8、引入(introduction)

在不修改代碼的前提下,引入可以在運行期為類動態(tài)地添加一些方法或字段

Spring 實現AOP所需要的包:

1、Spring提供的jar包

2、aopalliance.jar

3、aspectjweaver.jar

Spring 實現AOP的方式:

1、Java動態(tài)代理

該方法針對接口的實例創(chuàng)建代理

applicationContext.xml的配置如下:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xmlns:tx="http://www.springframework.org/schema/tx" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> 
     
    <bean id="concreteImplementor" class="com.marving.aop.ConcreteImplementor" /> 
  
    <bean id="interceptorHandler" class="com.marving.aop.InterceptorHandler" /> 
     
    <aop:config> 
      <aop:aspect id="interceptor" ref="interceptorHandler"> 
        <aop:pointcut id="addAllMethod" expression="execution(* com.marving.aop.Abstration.*(..))" /> 
        <aop:before method="doSomething" pointcut-ref="addAllMethod" /> 
        <aop:after method="doSomething" pointcut-ref="addAllMethod" /> 
      </aop:aspect> 
    </aop:config> 
</beans> 

其中Abstration為接口,ConcreteImplementor為實現類,InterceptorHandler為代理攔截類。

public interface <span >Abstration</span> { 
  public void operation() 
} 
//具體實現化角色 
public class ConcreteImplementor implements Implementor{ 
 
  @Override 
  public void operation() {   
    System.out.println("ConcreteImplementor"); 
  } 
 
} 
public class InterceptorHandler{  
  public void printTime(){ 
    System.out.println("CurrentTime = " + System.currentTimeMillis()); 
  } 
} 

2、CGLIB生成代理

CGLIB針對代理對象為類的情況使用。

通過實現MethodInterceptor接口,并實現 public Object intercept(Object obj, Method m, Object[] args,MethodProxy proxy) throws Throwable方法生成代理。

3、BeanNameAutoProxyCreator實現AOP

Spring為我們提供了自動代理機制,讓容器為我們自動生成代理,把我們從煩瑣的配置工作中解放出來,在內部,Spring 使用BeanPostProcessor自動地完成這項工作。

具體配置如下: 

<bean id="MyInterceptor" class="com.yesjpt.interceptor. MyInterceptor"></bean>  
<bean  
  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  <property name="beanNames">  
    <list>  
      <value>*Service</value>  
    </list>  
  </property>  
  <property name="interceptorNames">  
    <list>  
      <value>MyInterceptor</value>  
    </list>  
  </property>  
 </bean> 

其中*Service 為需要攔截代理的bean,以Service結尾的都 被攔截,并使用MyInterceptor 進行攔截,可配置多個攔截器,按順序執(zhí)行。

import java.lang.reflect.Method; 
import org.aopalliance.intercept.MethodInterceptor; 
import org.aopalliance.intercept.MethodInvocation; 
/** 
 * @author 
 * 
 */  
public class MyInterceptor implements MethodInterceptor{  
  
  @Override  
  public Object invoke(MethodInvocation invocation) throws Throwable {  
      
    Method method = invocation.getMethod();//獲取被攔截的方法  
    Object[] arguments = invocation.getArguments();//獲取攔截方法的參數  
    /* 
     * 特殊,某些權限需要做特殊處理 
     * 比如用戶信息權限,在方法執(zhí)行完畢返回的時候,要將電話號碼與郵箱抹除 
     */  
    //環(huán)繞通知前置特殊處理  
    this.beforeReslove();  
    Object proceed = invocation.proceed();//調用目標方法  
    //環(huán)繞通知后置特殊處理  
    proceed = this.afterReslove();  
    return proceed;  
  } 
   private Object afterReslove() { 
      System.out.println("CurrentTime = " + System.currentTimeMillis()); 
     return null; 
   } 
   private void beforeReslove() { 
      System.out.println("CurrentTime = " + System.currentTimeMillis()); 
   }   
} 

4、使用注解AspectJ實現AOP

ApplicationContext.xml 加入

<aop:aspectj-autoproxy/> 

 創(chuàng)建切面處理類

package com.marving.aop; 
import java.util.Arrays; 
import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Pointcut; 
import org.springframework.stereotype.Component;  
@Aspect 
@Component  
public class AspectHandler { 
   
  @Pointcut("execution(* com.marving.service.BaseServ+.*(..))") 
  private void doMethod() { 
  } 
 
    /** 
   * This is the method which I would like to execute before a selected method 
   * execution. 
   */ 
  @Before("doMethod()") 
  public void beforeAdvice() { 
    System.out.println("before method invoked."); 
  } 
 
  /** 
   * This is the method which I would like to execute after a selected method 
   * execution. 
   */ 
  @After("doMethod()") 
  public void afterAdvice() { 
    System.out.println("after method invoked."); 
  } 
 
  // 配置controller環(huán)繞通知,使用在方法aspect()上注冊的切入點 
  @Around("doMethod()") 
  public Object around(ProceedingJoinPoint pjp) throws Throwable{ 
    Object result = null; 
    String methodName = pjp.getSignature().getName(); 
    try { 
      System.out.println("The method [" + methodName + "] begins with " + Arrays.asList(pjp.getArgs())); 
      result = pjp.proceed(); 
    } catch (Throwable e) { 
      System.out.println("The method [" + methodName + "] occurs expection : " + e); 
      throw new RuntimeException(e); 
    } 
    System.out.println("The method [" + methodName + "] ends"); 
    return result; 
  } 
} 

通過表達式execution(* com.marving.service.BaseServ+.*(..)) 匹配切入點函數,并使用@Before@After@Around 對所攔截方法執(zhí)行前、中、后進行攔截并執(zhí)行處理函數。

@Around @Before @After三個注解的區(qū)別@Before是在所攔截方法執(zhí)行之前執(zhí)行一段邏輯。@After 是在所攔截方法執(zhí)行之后執(zhí)行一段邏輯。@Around是可以同時在所攔截方法的前后執(zhí)行一段邏輯。

值得注意的是,Around在攔截方法后,需要返回一個方法執(zhí)行結果,否則,原方法不能正常執(zhí)行。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI