Spring AOP

gung123發表於2020-02-18

AOP中的概念

Aspect(切面):指橫切性關注點的抽象即為切面,它與類相似,只是兩者的關注點不一樣,類是對物體特徵的抽象,而切面是橫切性關注點的抽象(包括切入點的描述和通知的描述)。

Joinpoint(連線點):所謂連線點是指那些被攔截到的點。在spring中,這些點指的是方法,
因為spring只支援方法型的連線點,實際上joinpoint還可以是field或者構造器。

Pointcut(切入點):所謂切入點是指我們要對那些joinpoint進行攔截的定義。

Advice(通知):所謂通知是指攔截到jointpoint之後所要做的事情就是通知。通知分為前置通知、後置通知、異常通知、最終通知、環繞通知。
瞭解springcloud架構可以加求求:三五三六二四七二五九

Target(目標物件):代理的目標物件

Weave(織入): 指將aspects應用到target物件並導致proxy物件建立的過程稱為織入

Introducton(引入):在不修改類程式碼的前提下,Introduction可以在執行期為類動態地新增一些方法或Field

Spring提供了兩種切面使用方式,實際工作中我們可以選用其中一種

1 基於xml配置方式進行AOP開發
2 基於註解方式進行AOP開發  

(一)基於註解的方式

下面是基於註解的方式

Java程式碼

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns=
"
       xmlns:xsi=
"
       xmlns:context=
"
       xmlns:aop=
"
       xsi:schemaLocation="http:
//
           http:
//
           http:
//
           http:
//
        <aop:aspectj-autoproxy/><!-- 啟動對
@AspectJ註解的支援 -->  
</beans>

Java程式碼

import org.aspectj.lang.ProceedingJoinPoint;  
import org.aspectj.lang.annotation.After;  
import org.aspectj.lang.annotation.AfterReturning;  
import org.aspectj.lang.annotation.AfterThrowing;  
import org.aspectj.lang.annotation.Around;  
import org.aspectj.lang.annotation.Aspect;  
import org.aspectj.lang.annotation.Before;  
import org.aspectj.lang.annotation.Pointcut;  
import org.springframework.stereotype.Component;  
  
@Aspect @Component  
public class MyInterceptor {  
  
/** 
     *@Pointcut :表示規定切入點  
     *execution() 語法規範 
     * 第一個“*”表示任意返回結果型別 
     * “cn.itcast.service.impl.PersonServiceBean”:表示對此類進行攔截, 
     * 如果是cn.itcast.service..*.*:表示對包cn.itcast.service以及子包裡所 
有的類的所有方法進行攔截, 
     * (..)表示引數  
     */   
  
      
    
@Pointcut("execution(* com.mingbai.springaop.PersonServiceBean.*(..))")  
    
private void anyMethod(){}//宣告一個切入點  
      
/*  @Before("anyMethod()") 
    public void doAccessCheck(){ 
        System.out.println("前置通知"); 
    }*/  
      
    
//此時的前置通知,只能攔截到引數個數和型別匹配的方法  
    
//args(name)中的name必須和方法doAccessCheck的引數一至  
    
@Before("anyMethod() && args(name)")  
    
public void doAccessCheck(String name){  
        System.out.println(name+
"前置通知");  
    }  
      
/*  @AfterReturning("anyMethod()") 
    public void doAfterReturn(){ 
        System.out.println("後置通知"); 
    }*/  
    
//得到方法的返回值  
    
@AfterReturning(pointcut="anyMethod()",returning="result")  
    
public void doAfterReturn(String result){  
        System.out.println(
"後置通知  "+result);  
    }  
      
  
    
@After("anyMethod()")  
    
public void doAfter(){  
        System.out.println(
"最終通知");  
    }  
      
/*  @AfterThrowing("anyMethod()") 
    public void doAfterThrow(){ 
        System.out.println("異常通知"); 
    }*/  
    
@AfterThrowing(pointcut="anyMethod()",throwing="e")  
    
public void doAfterThrow(Exception e){  
        System.out.println(
"異常通知------"+e.getMessage());  
    }  
      
    
@Around("anyMethod()")  
    
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{  
        System.out.println(
"環繞通知  開始");  
        Object obj = pjp.proceed();  
        System.out.println(
"環繞通知  結束");  
        
return obj;  
    }  
}

(二)基於xml配置檔案的

切面只是一個普通的javabean

Java程式碼 

  1. import org.aspectj.lang.ProceedingJoinPoint;  
  2.   
  3. public class MyInterceptor1 {  
  4.       
  5.   
  6.      public void doAccessCheck(){  
  7.         System.out.println( "前置通知-------");  
  8.     }  
  9.       
  10.      public void doAfterReturn(){  
  11.         System.out.println( "後置通知");  
  12.     }  
  13.       
  14.   
  15.      public void doAfter(){  
  16.         System.out.println( "最終通知");  
  17.     }  
  18.      public void doAfterThrow(){  
  19.         System.out.println( "異常通知");  
  20.     }  
  21.       
  22.      public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{  
  23.         System.out.println( "環繞通知  開始");  
  24.         Object obj = pjp.proceed();  
  25.         System.out.println( "環繞通知  結束");  
  26.          return obj;  
  27.     }  
  28. }  


配置檔案 :

Java程式碼 

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns=
"
       xmlns:xsi=
"
       xmlns:context=
"
       xmlns:aop=
"
       xmlns:tx=
"
       xsi:schemaLocation="http:
//
           http:
//
           http:
//
           http:
//
           http:
//
       
   
[color=brown]     <bean id=
"per" class="com.mingbai.springaop.PersonServiceBean"/>  
     <bean id=
"myInterceptor" class="com.mingbai.springaop.MyInterceptor1"/>  
     <!--    
     <aop:config>  
        <aop:aspect id=
"asp" ref="myInterceptor">  
            <aop:pointcut id=
"mycut" expression="execution(* com.mingbai.springaop.*.*(..))"/>  
            <aop:before pointcut-ref=
"mycut" method="doAccessCheck"/>  
            <aop:after-returning pointcut-ref=
"mycut" method="doAfterReturn"/>  
            <aop:after pointcut-ref=
"mycut" method="doAfter"/>  
            <aop:after-throwing pointcut-ref=
"mycut" method="doAfterThrow"/>  
            <aop:around pointcut-ref=
"mycut" method="doBasicProfiling"/>  
        </aop:aspect>  
     </aop:config>[/color]  
     -->   
     <!-- 只是攔截返回型別為java.lang.String的方法     
     <aop:config>  
        <aop:aspect id=
"asp" ref="myInterceptor">  
            <aop:pointcut id=
"mycut" expression="execution(java.lang.String com.mingbai.springaop.*.*(..))"/>  
            <aop:before pointcut-ref=
"mycut" method="doAccessCheck"/>  
            <aop:after-returning pointcut-ref=
"mycut" method="doAfterReturn"/>  
            <aop:after pointcut-ref=
"mycut" method="doAfter"/>  
            <aop:after-throwing pointcut-ref=
"mycut" method="doAfterThrow"/>  
            <aop:around pointcut-ref=
"mycut" method="doBasicProfiling"/>  
        </aop:aspect>  
     </aop:config>  
   -->   
   <!-- 返回非
void的方法 -->  
   <aop:config>  
        <aop:aspect id=
"asp" ref="myInterceptor">  
            <aop:pointcut id=
"mycut" expression="execution(!void com.mingbai.springaop.*.*(..))"/>  
            <aop:before pointcut-ref=
"mycut" method="doAccessCheck"/>  
            <aop:after-returning pointcut-ref=
"mycut" method="doAfterReturn"/>  
            <aop:after pointcut-ref=
"mycut" method="doAfter"/>  
            <aop:after-throwing pointcut-ref=
"mycut" method="doAfterThrow"/>  
            <aop:around pointcut-ref=
"mycut" method="doBasicProfiling"/>  
        </aop:aspect>  
     </aop:config>  
   <!-- 匹配第一個引數為java.lang.String,其它的無所謂   
     <aop:config>  
        <aop:aspect id=
"asp" ref="myInterceptor">  
            <aop:pointcut id=
"mycut" expression="execution(* com.mingbai.springaop.*.*(..))"/>  
            <aop:before pointcut-ref=
"mycut" method="doAccessCheck"/>  
            <aop:after-returning pointcut-ref=
"mycut" method="doAfterReturn"/>  
            <aop:after pointcut-ref=
"mycut" method="doAfter"/>  
            <aop:after-throwing pointcut-ref=
"mycut" method="doAfterThrow"/>  
            <aop:around pointcut-ref=
"mycut" method="doBasicProfiling"/>  
        </aop:aspect>  
     </aop:config>  
   -->  
     
</beans>

轉載於: https://www.cnblogs.com/junjiang3/p/9033038.html

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69952307/viewspace-2676042/,如需轉載,請註明出處,否則將追究法律責任。