spring學習(五)詳細介紹AOP
AOP稱為面向切面程式設計,在程式開發中主要用來解決一些系統層面上的問題,比如日誌,事務,許可權等待
它利用一種稱為"橫切"的技術,剖解開封裝的物件內部,並將那些影響了多個類的公共行為封裝到一個可重用模組,並將其命名為"Aspect",即切面。所謂"切面",簡單說就是那些與業務無關,卻為業務模組所共同呼叫的邏輯或責任封裝起來,便於減少系統的重複程式碼,降低模組之間的耦合度,並有利於未來的可操作性和可維護性。
舉個例子:
Spring對AOP的支援
Spring中AOP代理由Spring的IOC容器負責生成、管理,其依賴關係也由IOC容器負責管理
1、預設使用Java動態代理來建立AOP代理,這樣就可以為任何介面例項建立代理了
2、當需要代理的類不是代理介面的時候,Spring會切換為使用CGLIB代理,也可強制使用CGLIB
AOP程式設計其實是很簡單的事情,縱觀AOP程式設計,程式設計師只需要參與三個部分:
1、定義普通業務元件
2、定義切入點,一個切入點可能橫切多個業務元件
3、定義增強處理,增強處理就是在AOP框架為普通業務元件織入的處理動作
所以進行AOP程式設計的關鍵就是定義切入點和定義增強處理,一旦定義了合適的切入點和增強處理,AOP框架將自動生成AOP代理,即:代理物件的方法=增強處理+被代理物件
簡單說,就是spring能幫我們建立代理物件!!!
需要了解的幾個概念:
幾個概念
(1)Aspect(切面):通常是一個類,裡面可以定義切入點和通知
(2)JointPoint(連線點):程式執行過程中明確的點,一般是方法的呼叫
(3)Advice(通知):AOP在特定的切入點上執行的增強處理,有before,after,afterReturning,afterThrowing,around
(4)Pointcut(切入點):就是帶有通知的連線點,在程式中主要體現為書寫切入點表示式
(5)AOP代理:AOP框架建立的物件,代理就是目標物件的加強。Spring中的AOP代理可以使JDK動態代理,也可以是CGLIB代理,前者基於介面,後者基於子類
如下圖:
一、導包(用的是IDEA的maven工程,pom.xml匯入依賴,注意jar包的版本,會導致出錯)
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>day2</groupId> <artifactId>day2</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> <!--測試jar包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.6.RELEASE</version> </dependency> </dependencies> </project>
二、書寫通知類
import org.aspectj.lang.ProceedingJoinPoint; public class MyAdivce { //前置通知 // |-目標方法執行之前呼叫 //後置通知(如果出現異常不會呼叫) // |-在目標方法執行之後呼叫 //環繞通知 // |-在目標方法之前和之後都呼叫 //異常攔截通知 // |-如果出現異常,就會呼叫 //後置通知(無論是否出現 異常都會呼叫) // |-在目標方法執行之後呼叫 //---------------------------------------------------------------- //前置通知 public void before(){ System.out.println("這是前置通知!!"); } //後置通知 public void afterReturning(){ System.out.println("這是後置通知(如果出現異常不會呼叫)!!"); } //環繞通知 public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("這是環繞通知之前的部分!!"); Object proceed = pjp.proceed();//呼叫目標方法 System.out.println("這是環繞通知之後的部分!!"); return proceed; } //異常通知 public void afterException(){ System.out.println("出事啦!出現異常了!!"); } //後置通知 public void after(){ System.out.println("這是後置通知(出現異常也會呼叫)!!"); } }
三、書寫要增強的介面和它的實現類
public interface UserService { void save(); void delete(); void update(); void find(); }
public class UserServiceImpl implements UserService { public void save() { } public void delete() { } public void update() { } public void find() { } }
四、書寫配置檔案
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> <!-- 準備工作: 匯入aop(約束)名稱空間 --> <!-- 1.配置目標物件 --> <bean name="userService" class="dyh.service.UserServiceImpl" ></bean> <!-- 2.配置通知物件 --> <bean name="myAdvice" class="dyh.aopdemo.MyAdivce" ></bean> <!-- 3.配置將通知織入目標物件 --> <aop:config> <!-- 配置切入點 public void cn.itcast.service.UserServiceImpl.save() void cn.itcast.service.UserServiceImpl.save() * cn.itcast.service.UserServiceImpl.save() * cn.itcast.service.UserServiceImpl.*() * cn.itcast.service.*ServiceImpl.*(..) * cn.itcast.service..*ServiceImpl.*(..) --> <aop:pointcut expression="execution(* dyh.service.*ServiceImpl.*(..))" id="pc"/> <aop:aspect ref="myAdvice" > <!-- 指定名為before方法作為前置通知 --> <aop:before method="before" pointcut-ref="pc" /> <!-- 後置 --> <aop:after-returning method="afterReturning" pointcut-ref="pc" /> <!-- 環繞通知 --> <aop:around method="around" pointcut-ref="pc" /> <!-- 異常攔截通知 --> <aop:after-throwing method="afterException" pointcut-ref="pc"/> <!-- 後置 --> <aop:after method="after" pointcut-ref="pc"/> </aop:aspect> </aop:config> </beans>
五、測試
import dyh.service.UserService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:dyh/aopdemo/applicationContext.xml") public class Demo { //@Autowired @Resource(name = "userService") private UserService us; @Test public void fun(){ us.save(); } }
測試結果: