Spring詳解之(AOP)

概述

1.什麼是AOP(面向切面編程)

AOP(Aspect Oriented Programming)意爲:面向切面編程,通過預編譯方式和運行期動態代理實現程序功能的統一維護的一種技術。
AOP是OOP的延續,是軟件開發中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生範型。
利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發的效率。

2.AOP的業務邏輯

在不改變代碼的情況下,添加新的功能
在這裏插入圖片描述

3.AOP在spring中的作用(基本術語)

  • 切面(Aspect):橫切關注點 被模塊化 的特殊對象。即,它是一個類。
  • 目標對象(Target):指將要被增強的對象,即包含主業務邏輯的類對象。
  • 連接點(JoinPoint):與切入點匹配的執行點。
  • 切入點(PointCut):切面通知 執行的 “地點”的定義。
  • 通知(Advice):切面必須要完成的工作。即,它是類中的一個方法。
  • 織入(Weaving):織入是將切面和業務邏輯對象連接起來, 並創建通知代理的過程。
  • 增強器(Adviser):Advisor由切入點和Advice組成。
  • 代理(Proxy):向目標對象應用通知之後創建的對象。

4.五種類型的Advice及接口

在這裏插入圖片描述

5.XML中AOP的配置元素

在這裏插入圖片描述

6.註解中的AOP指令

在這裏插入圖片描述

使用AOP編程的三種方式

1.第一種方式:使用Spring的API 接口

(1)導包,添加AOP依賴
<dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.4</version>
        </dependency>
(2)編寫UserSerive接口及實現類
package Serive;
public interface UserSerive {
       public void add();
       public void delete();
       public void update();
       public void searth();
}
package Serive;
public class Impl implements UserSerive {
    public void add() { System.out.println("增加用戶"); }
    public void delete() { System.out.println("刪除用戶"); }
    public void update() { }
    public void searth() { }
}
(3)編寫兩個插入類,實現AOP接口
package Serive.Config;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
    //method 要執行的目標方法
    //objects:要調用這方法的參數
    //o:要調用的目標對象
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(o.getClass().getName()+"de "+ method.getName()+"");
    }
}
package Serive.Config;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class LogAfter implements AfterReturningAdvice {
    public void afterReturning(Object o1, Method method, Object[] objects, Object o) throws Throwable {//o1:返回值
        System.out.println(o.getClass().getName()+"de "+ method.getName()+""+o1);
    }
}
(4)在XML中配置AOP
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd       
         http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
">
 <bean id="UserSerive" class="Serive.Impl"></bean>
    <bean id="Log" class="Serive.Config.Log"></bean>
    <bean id="LogAfter" class="Serive.Config.LogAfter"></bean>
    <!--espression:類名全稱 public com.service.方法-->
    <aop:config>
        <aop:pointcut id="pt" expression="execution(* Serive.Impl.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="Log" pointcut-ref="pt"></aop:advisor>
        <aop:advisor advice-ref="LogAfter" pointcut-ref="pt"></aop:advisor>
    </aop:config>
    </beens>

注意:添加AOP命令空間

(5)測試運行
import Serive.UserSerive;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Text {
    @Test
    public void Text1(){
        ApplicationContext Context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        UserSerive userSerive = (UserSerive) Context.getBean("UserSerive");
        userSerive.add();
    }
}

2.第二種方式:自定義來實現AOP

  • 比第一種方式簡單,只需要編寫一個插入類
package Serive.Config;
public class diy {
    public void before(){        System.out.println("=前面========");    }
    public  void after(){        System.out.println("==後面====");    }
    }
}
  • 在XML中配置AOP
<bean id="UserSerive" class="Serive.Impl"></bean>
    <bean id="diy" class="Serive.Config.diy"></bean>
    <aop:config>
        <aop:aspect ref="diy">
        <aop:pointcut id="pt" expression="execution(* Serive.Impl.*(..))"></aop:pointcut>
         <aop:before method="before" pointcut-ref="pt"></aop:before>
            <aop:after method="after" pointcut-ref="pt"></aop:after>
        </aop:aspect>
    </aop:config>

注意:添加AOP命令空間

3.第三種方式:使用註解

  • 編寫一個插入類
package Serive.Config;
        import org.aspectj.lang.ProceedingJoinPoint;
        import org.aspectj.lang.annotation.After;
        import org.aspectj.lang.annotation.Around;
        import org.aspectj.lang.annotation.Aspect;
        import org.aspectj.lang.annotation.Before;
@Aspect
public class diy {
    @Before("execution(* Serive.Impl.*(..))")
    public void before(){
        System.out.println("=前面========");
    }
    @After("execution(* Serive.Impl.*(..))")
    public  void after(){
        System.out.println("==後面====");
    }
    @Around("execution(* Serive.Impl.*(..))")
    public  void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("環繞前");
        System.out.println(joinPoint.getSignature());//執行的方法是哪一個
        Object proceed = joinPoint.proceed();//執行方法
        System.out.println("環繞後");
        System.out.println(proceed);
    }
}
  • 在XML中進行配置
 <aop:aspectj-autoproxy/>     //自動識別註解,必須加上
    <bean id="diy" class="Serive.Config.diy"></bean>
     <bean id="UserSerive" class="Serive.Impl"></bean>

注意:添加AOP命令空間
運行結果:
在這裏插入圖片描述

先執行環繞的方法,執行before方法,最後執行after方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章