Spring AOP面向切面程式設計實現原理方法詳解
1. 什麼是AOP
AOP (Aspect Oriented Programming)意為:面向切面程式設計,通過預編譯方式和執行期動態代理實現在不修改原始碼的情況下,給程式動態統一新增功能的一種技術,可以理解成動態代理。是Spring框架中的一個重要內容。利用 AOP 可以對業務邏輯的各個部分進行隔離,使業務邏輯各部分之間的耦合度降低,提高程式的可重用性,同時提高開發的效率
2. Spring AOP
①. AOP 在Spring中的作用
提供宣告式事務;允許使用者自定義切面
②. AOP 的基本概念
橫切關注點:跨越應用程式多個模組的方法或功能。即與我們業務邏輯無關,但需要我們關注的部分就是橫切關注點。如日誌,安全,快取,事務等等 ....
- Aspect(切面):橫切關注點被模組化的特殊物件。通常是一個類,裡面可以定義切入點和通知
- Weaving(織入):把切面(aspect)連線到其它的應用程式型別或者物件上,並建立一個被通知(advised)的物件。 這些可以在編譯時,類載入時和執行時完成。Spring和其它純Java AOP框架一樣,在執行時完成織入
- Advice(通知):AOP在特定的切入點上執行的增強處理,是切面必須要完成的工作,也是類中的一個方法
- Target(目標):被通知物件
- AOP(代理):AOP框架建立的物件,代理就是目標物件的加強。Spring中的 AOP 代理可以是 JDK 動態代理,也可以是 CGLIB 代理,前者基於介面,後者基於子類
- JointPoint(連線點):程式執行過程中明確的點,一般是方法的呼叫
- Pointcut(切入點):就是帶有通知的連線點,與切入點匹配的執行點
③. 使用Spring實現Aop
前提
使用AOP織入,需要匯入一個依賴包
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.5</version> </dependency>
實現Aop的三種方式
方式一:通過 Spring API 實現【主要是springAPI介面實現】
首先編寫業務介面和實現類
public interface UserService { public void add(); public void delete(); public void update(); public void search(); }
public class UserServiceImpl implements UserService{ public void add() { System.out.println("增加了一個使用者"); } public void delete() { System.out.println("刪除了一個使用者"); } public void update() { System.out.println("更新了一個使用者"); } public void select() { System.out.println("查詢了一個使用者"); } }
接著編寫增強類,這裡寫兩個:前置增強Log和後置增強AfterLog
import org.springframework.aop.MethodBeforeAdvice; import java.lang.reflect.Method; public class Log implements MethodBeforeAdvice { //method: 要執行的目標物件的方法 //args: 引數 //target: 目標物件 public void before(Method method,Object[] args,Object target) throws Throwable { System.out.println(target.getClass().getName()+"的"+method.getName()+"被執行了"); } }
import org.springframework.aop.AfterReturningAdvice; import java.lang.reflect.Method; public class AfterLog implements AfterReturningAdvice { //returnValue;返回值 public void afterReturning(Object returnValue,Method method,Object target) throws Throwable { System.out.println("執行了"+method.getName()+"方法,返回結果為:"+returnValue); } }
最後在Spring的檔案中註冊( applicationContext.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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--註冊bean--> <bean id="userService" class="com.lf.service.UserServiceImpl"/> <bean id="log" class="com.lf.log.Log"/> <bean id="afterLog" class="com.lf.log.AfterLog"/> <!--方式一:使用原生Spring API介面 --> <!--配置aop:需要匯入aop的約束--> <aop:config> <!--切入點:expression:表示式,execution(要執行的位置! * * * * *) --> <aop:pointcut id="pointcut" expression="execution(* com.lf.service.UserServiceImpl.*(..))"/> <!--執行環繞; advice-ref執行方法 . pointcut-ref切入點--> <aop:advisor advice-ref="log" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/> </aop:config> </beans>
進行測試:
import com.lf.service.UserService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService1 = (UserService) context.getBean("userService"); UserService userService = (UserService) context.getBean("userService"); userService.add(); } }
執行結果:
com.lf.service.UserServiceImpl的add被執行了
增加了一個使用者
執行了add方法,返回結果為:null
方式二:自定義類實現AOP【主要是切面定義】
目標業務類不變,還是方式一中的UserServiceImpl
寫入一個切入類
public class DiyPointCut { public void before(){ System.out.println("========方法執行前========="); } public void after(){ System.out.println("========方法執行後========="); } }
在Spring中配置(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" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--註冊bean--> <bean id="userService" class="com.lf.service.UserServiceImpl"/> <bean id="log" class="com.lf.log.Log"/> <bean id="afterLog" class="com.lf.log.AfterLog"/> <!--方式二:自定義類--> <bean id="diy" class="com.lf.diy.DiyPointCut"/> <aop:config> <!--自定義切面, ref 要引用的類--> <aop:aspect ref="diy"> <!--切入點--> <aop:pointcut id="point" expression="execution(* com.lf.service.UserServiceImpl.*(..))"/> <!--通知--> <aop:before method="before" pointcut-ref="point"/> <aop:after method="after" pointcut-ref="point"/> </aop:aspect> </aop:config> </beans>
在上面的 MyTest.java 中測試,得到結果:
========方法執行前=========
增加了一個使用者
========方法執行後=========
方式三:使用註解實現【多用】
編寫一個註解實現的增強類
package com.lf.diy; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; 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 AnnotationPointCut { @Before("execution(* com.lf.service.UserServiceImpl.*(..))") public void before(){ System.out.println("=====方法執行前======"); } @After("execution(* com.lf.service.UserServiceImpl.*(..))") public void after(){ System.out.println("=====方法執行後======"); } //在環繞增強中,我們可以給定一個引數,代表我們要獲取處理切入的點; @Around("execution(* com.lf.service.UserServiceImpl.*(..))") public void around(ProceedingJoinPoint jp) throws Throwable { System.out.println("環繞前"); Signature signature = jp.getSignature();//獲得簽名 System.out.println("signature:"+signature); Object proceed = jp.proceed(); //執行方法 System.out.println("環繞後"); System.out.println(proceed); } }
在Spring配置檔案中,註冊bean,並增加支援註解的配置
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!--註冊bean--> <bean id="userService" class="com.lf.service.UserServiceImpl"/> <bean id="log" class="com.lf.log.Log"/> <bean id="afterLog" class="com.lf.log.AfterLog"/> <!--方式三--> <bean id="annotationPointCut" class="com.lf.diy.AnnotationPointCut"/> <!--開啟註解支援! JDK(預設 proxy-target-class="false") cglib(proxy-target-class="true")--> <aop:aspectj-autoproxy/> </beans>
在 MyTest.java 中測試
import com.lf.service.UserService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest { @Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) context.getBean("userService"); userService.add(); } }
得到結果:
環繞前
signature:void com.lf.service.UserService.add()
=====方法執行前======
增加了一個使用者
=====方法執行後======
環繞後
null
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。