SpringAOP的實現原理
AOP
AOP(Aspect Oriented Programming),即面向切面程式設計,可以說是OOP(Object Oriented Programming,面向物件程式設計)的補充和完善。OOP引入封裝、繼承、多型等概念來建立一種物件層次結構,用於模擬公共行為的一個集合。不過OOP允許開發者定義縱向的關係,但並不適合定義橫向的關係,例如日誌功能。日誌程式碼往往橫向地散佈在所有物件層次中,而與它對應的物件的核心功能毫無關係對於其他型別的程式碼,如安全性、異常處理和透明的持續性也都是如此,這種散佈在各處的無關的程式碼被稱為橫切(cross cutting),在OOP設計中,它導致了大量程式碼的重複,而不利於各個模組的重用。
AOP技術恰恰相反,它利用一種稱為"橫切"的技術,剖解開封裝的物件內部,並將那些影響了多個類的公共行為封裝到一個可重用模組,並將其命名為"Aspect",即切面。所謂"切面",簡單說就是那些與業務無關,卻為業務模組所共同呼叫的邏輯或責任封裝起來,便於減少系統的重複程式碼,降低模組之間的耦合度,並有利於未來的可操作性和可維護性。
使用"橫切"技術,AOP把軟體系統分為兩個部分:核心關注點和橫切關注點。業務處理的主要流程是核心關注點,與之關係不大的部分是橫切關注點。橫切關注點的一個特點是,他們經常發生在核心關注點的多處,而各處基本相似,比如許可權認證、日誌、事物。AOP的作用在於分離系統中的各種關注點,將核心關注點和橫切關注點分離開來。
AOP核心概念
1、橫切關注點
對哪些方法進行攔截,攔截後怎麼處理,這些關注點稱之為橫切關注點
<aop:pointcut id="addAllMethod" expression="execution(* com.xrq.aop.HelloWorld.*(..))" />通過expression定義所有HelloWorld包下的類為橫切關注點
2、切面(aspect)
類是對物體特徵的抽象,切面就是對橫切關注點的抽象
3、連線點(joinpoint)
被攔截到的點,因為Spring只支援方法型別的連線點,所以在Spring中連線點指的就是被攔截到的方法,實際上連線點還可以是欄位或者構造器
Helloword包下所有類的方法
4、切入點(pointcut)
我們關注的連線點就是切入點。
5、通知(advice)
所謂通知指的就是指攔截到連線點之後要執行的程式碼,通知分為前置、後置、異常、最終、環繞通知五類
6、目標物件
代理的目標物件
7、織入(weave)
將切面應用到目標物件並導致代理物件建立的過程
8、引入(introduction)
在不修改程式碼的前提下,引入可以在執行期為類動態地新增一些方法或欄位
Spring對AOP的支援
Spring中AOP代理由Spring的IOC容器負責生成、管理,其依賴關係也由IOC容器負責管理。因此,AOP代理可以直接使用容器中的其它bean例項作為目標,這種關係可由IOC容器的依賴注入提供。Spring建立代理的規則為:
1、預設使用Java動態代理來建立AOP代理,這樣就可以為任何介面例項建立代理了
2、當需要代理的類不是代理介面的時候,Spring會切換為使用CGLIB代理,也可強制使用CGLIB
AOP程式設計其實是很簡單的事情,縱觀AOP程式設計,程式設計師只需要參與三個部分:
1、定義普通業務元件
2、定義切入點,一個切入點可能橫切多個業務元件
3、定義增強處理,增強處理就是在AOP框架為普通業務元件織入的處理動作
所以進行AOP程式設計的關鍵就是定義切入點和定義增強處理,一旦定義了合適的切入點和增強處理,AOP框架將自動生成AOP代理,即:代理物件的方法=增強處理+被代理物件的方法。
下面給出一個Spring AOP的.xml檔案模板,名字叫做aop.xml,之後的內容都在aop.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"> </beans>
基於Spring的AOP簡單實現
注意一下,在講解之前,說明一點:使用Spring AOP,要成功執行起程式碼,只用Spring提供給開發者的jar包是不夠的,請額外上網下載兩個jar包:
1、aopalliance.jar
2、aspectjweaver.jar
開始講解用Spring AOP的XML實現方式,先定義一個介面:
public interface HelloWorld { void printHelloWorld(); void doPrint(); }
定義兩個介面實現類:
public class HelloWorldImpl1 implements HelloWorld { public void printHelloWorld() { System.out.println("Enter HelloWorldImpl1.printHelloWorld()"); } public void doPrint() { System.out.println("Enter HelloWorldImpl1.doPrint()"); return ; } }
public class HelloWorldImpl2 implements HelloWorld { public void printHelloWorld() { System.out.println("Enter HelloWorldImpl2.printHelloWorld()"); } public void doPrint() { System.out.println("Enter HelloWorldImpl2.doPrint()"); return ; } }
橫切關注點,這裡是列印時間:
public class TimeHandler { public void printTime() { System.out.println("CurrentTime = " + System.currentTimeMillis()); } }
有這三個類就可以實現一個簡單的Spring AOP了,看一下aop.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="helloWorldImpl1" class="com.xrq.aop.HelloWorldImpl1" /> <bean id="helloWorldImpl2" class="com.xrq.aop.HelloWorldImpl2" /> <bean id="timeHandler" class="com.xrq.aop.TimeHandler" /> <aop:config> <aop:aspect id="time" ref="timeHandler"> <aop:pointcut id="addAllMethod" expression="execution(* com.xrq.aop.HelloWorld.*(..))" /> <aop:before method="printTime" pointcut-ref="addAllMethod" /> <aop:after method="printTime" pointcut-ref="addAllMethod" /> </aop:aspect> </aop:config> </beans>
寫一個main函式呼叫一下:
public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("aop.xml"); HelloWorld hw1 = (HelloWorld)ctx.getBean("helloWorldImpl1"); HelloWorld hw2 = (HelloWorld)ctx.getBean("helloWorldImpl2"); hw1.printHelloWorld(); System.out.println(); hw1.doPrint(); System.out.println(); hw2.printHelloWorld(); System.out.println(); hw2.doPrint(); }
執行結果為:
CurrentTime = 1446129611993 Enter HelloWorldImpl1.printHelloWorld() CurrentTime = 1446129611993 CurrentTime = 1446129611994 Enter HelloWorldImpl1.doPrint() CurrentTime = 1446129611994 CurrentTime = 1446129611994 Enter HelloWorldImpl2.printHelloWorld() CurrentTime = 1446129611994 CurrentTime = 1446129611994 Enter HelloWorldImpl2.doPrint() CurrentTime = 1446129611994
看到給HelloWorld介面的兩個實現類的所有方法都加上了代理,代理內容就是列印時間
基於Spring的AOP使用其他細節
1、增加一個橫切關注點,列印日誌,Java類為:
public class LogHandler { public void LogBefore() { System.out.println("Log before method"); } public void LogAfter() { System.out.println("Log after method"); } }
aop.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="helloWorldImpl1" class="com.xrq.aop.HelloWorldImpl1" /> <bean id="helloWorldImpl2" class="com.xrq.aop.HelloWorldImpl2" /> <bean id="timeHandler" class="com.xrq.aop.TimeHandler" /> <bean id="logHandler" class="com.xrq.aop.LogHandler" /> <aop:config> <aop:aspect id="time" ref="timeHandler" order="1"> <aop:pointcut id="addTime" expression="execution(* com.xrq.aop.HelloWorld.*(..))" /> <aop:before method="printTime" pointcut-ref="addTime" /> <aop:after method="printTime" pointcut-ref="addTime" /> </aop:aspect> <aop:aspect id="log" ref="logHandler" order="2"> <aop:pointcut id="printLog" expression="execution(* com.xrq.aop.HelloWorld.*(..))" /> <aop:before method="LogBefore" pointcut-ref="printLog" /> <aop:after method="LogAfter" pointcut-ref="printLog" /> </aop:aspect> </aop:config> </beans>
測試類不變,列印結果為:
CurrentTime = 1446130273734 Log before method Enter HelloWorldImpl1.printHelloWorld() Log after method CurrentTime = 1446130273735 CurrentTime = 1446130273736 Log before method Enter HelloWorldImpl1.doPrint() Log after method CurrentTime = 1446130273736 CurrentTime = 1446130273736 Log before method Enter HelloWorldImpl2.printHelloWorld() Log after method CurrentTime = 1446130273736 CurrentTime = 1446130273737 Log before method Enter HelloWorldImpl2.doPrint() Log after method CurrentTime = 1446130273737
要想讓logHandler在timeHandler前使用有兩個辦法:
(1)aspect裡面有一個order屬性,order屬性的數字就是橫切關注點的順序
(2)把logHandler定義在timeHandler前面,Spring預設以aspect的定義順序作為織入順序
2、我只想織入介面中的某些方法
修改一下pointcut的expression就好了:
<?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="helloWorldImpl1" class="com.xrq.aop.HelloWorldImpl1" /> <bean id="helloWorldImpl2" class="com.xrq.aop.HelloWorldImpl2" /> <bean id="timeHandler" class="com.xrq.aop.TimeHandler" /> <bean id="logHandler" class="com.xrq.aop.LogHandler" /> <aop:config> <aop:aspect id="time" ref="timeHandler" order="1"> <aop:pointcut id="addTime" expression="execution(* com.xrq.aop.HelloWorld.print*(..))" /> <aop:before method="printTime" pointcut-ref="addTime" /> <aop:after method="printTime" pointcut-ref="addTime" /> </aop:aspect> <aop:aspect id="log" ref="logHandler" order="2"> <aop:pointcut id="printLog" expression="execution(* com.xrq.aop.HelloWorld.do*(..))" /> <aop:before method="LogBefore" pointcut-ref="printLog" /> <aop:after method="LogAfter" pointcut-ref="printLog" /> </aop:aspect> </aop:config> </beans>
表示timeHandler只會織入HelloWorld介面print開頭的方法,logHandler只會織入HelloWorld介面do開頭的方法
3、強制使用CGLIB生成代理
前面說過Spring使用動態代理或是CGLIB生成代理是有規則的,高版本的Spring會自動選擇是使用動態代理還是CGLIB生成代理內容,當然我們也可以強制使用CGLIB生成代理,那就是<aop:config>裡面有一個"proxy-target-class"屬性,這個屬性值如果被設定為true,那麼基於類的代理將起作用,如果proxy-target-class被設定為false或者這個屬性被省略,那麼基於介面的代理將起作用。
相關推薦
spring框架學習筆記4:SpringAOP實現原理
odin 就是 sets 使用 point 攔截 ceo oca ssl AOP AOP(Aspect Oriented Programming),即面向切面編程,可以說是OOP(Object Oriented Programming,面向對象編程)的補充和完善。OOP引入
springAOP實現原理
all sed gis instance bubuko beans exce creator annotate spring AOP實現原理, spring 會在初始化的時候,創建一個BeanPostProcessor(AnnotationAwareAspectJAutoP
代理模式 -- SpringAOP的實現原理
網址連結:http://www.cnblogs.com/lcngu/p/5339555.html 代理模式UML圖: 代理模式中的角色: 1.抽象物件角色 聲明瞭目標類及代理類物件的共同介面,這樣在任何可以使用目標物件的地方都可以使用代理物件。 2.目標物件角色 定義了
jdk動態代理與cglib程式碼實現--SpringAop底層原理
動態代理分為兩類:基於介面的代理和基於繼承的代理 兩類實現的代表是:JDK代理 與 CGlib代理 cglib實現動態代理: 1、定義目標物件: public class RealSubject { //目標物件RealSubject,cglib不
SpringAOP的實現原理
AOP AOP(Aspect Oriented Programming),即面向切面程式設計,可以說是OOP(Object Oriented Programming,面向物件程式設計)的補充和完善
HashSet實現原理
運行 equal false spa 比較 logs pan ole print /* HashSet的實現原理: 往HashSet添加元素的時候,HashSet會先調用元素的hashCode方法得到元素的哈希值 , 然後通過元素 的哈希值經過移位等運算,就可以算出
mysql線程池的實現原理淺析
new one lose clear pre turn logs color 否則 今天抽空主要看了一下mysql線程池(cached threads)的實現原理,總體並不那麽復雜,也學到了一些設計原理,值得記錄一下。為了簡化代碼,讓思路更清晰,我刪去了不少錯誤處理,線程同
HashMap實現原理
一個 ash img 方法 shm 步長 初始 2的n次冪 http HashMap的數據結構是數組+單向鏈表,數組裏面存儲就是鏈表的Head節點,鏈表節點存儲的是我們put進去的key/value。 如果要實現HashMap,主要有三個重要的功能點: 1.初
(9)launcher3 之 外部 更換主題Theme APP demo 實現原理以及demo
解壓 work ace fontsize 思路 con 鎖屏 解壓文件夾 更新 先說下我的思路: luancher3裏面更換圖標的邏輯例如以下: 先從APP資源包裏查詢--數據庫查詢--其它地方查詢ICON 因此,我們僅僅須要把 從數據庫獲取ICON 代碼提前到 從A
Spring AOP 實現原理
pri ack more .net style 實現原理 cor http details Spring AOP 實現原理Spring AOP 實現原理
轉:深入Java集合學習系列:HashSet的實現原理
是否 abstract arc html 源代碼 cat param body static 0.參考文獻 深入Java集合學習系列:HashSet的實現原理 1.HashSet概述: HashSet實現Set接口,由哈希表(實際上是一個HashMap實例)支持。它
理解殺進程的實現原理(轉)
dir nullptr signed end 細節 信號signal tar res ets 基於Android 6.0的源碼剖析, 分析kill進程的實現原理,以及講講系統調用(syscall)過程,涉及源碼: /framework/base/core/java/a
atitit.文件上傳帶進度條的實現原理and組件選型and最佳實踐總結O7
private tps cto 協議 post sch 頁面 system osc atitit.文件上傳帶進度條的實現原理and組件選型and最佳實踐總結O7 1. 實現原理 1 2. 大的文件上傳原理::使用applet 1 3. 新的bp 2 1. 性能提升
深入分析Volatile的實現原理
queue 鏈接地址 什麽 高速緩存 spa 其中 帶來 系統內存 單詞 引言 在多線程並發編程中synchronized和Volatile都扮演著重要的角色,Volatile是輕量級的synchronized,它在多處理器開發中保證了共享變量的“可見性”。可見性的意思是當
django做服務端 window.name javascript跨域實現原理及實例
字符串 tex 並且 ble blog char src 兩個 splay 項目地址:https://github.com/blff122620/jsLibary/tree/master/crossDomainDemo 原理如下:window.name 傳輸技術,原本是 T
Gevent的協程實現原理
handle 保存 ont expires 了吧 理解 cal easy try 之前之所以看greenlet的代碼實現,主要就是想要看看gevent庫的實現代碼。。。然後知道了gevent的協程是基於greenlet來實現的。。。所以就又先去看了看greenlet的實
詳解單頁面路由的幾種實現原理
htm 缺點 服務 ajax請求參數 重復 情況 路由 dem history 路由是每個單頁面網站必須要有的,本篇基本不會天貼代碼,只講原理,代碼在頁面底會有github地址,註意,一定要放在本地服務器裏跑(因為有AJAX) 眾所周知,單頁面網站的路徑跳轉全是通過JS來控
幹貨 | 雲智慧透視寶Java代碼性能監控實現原理
ava jconsole 移動終端 雲智慧 指定 快速 cpu 架構 都是 這篇圖文並茂,高端大氣上檔次,思維縝密的文章,一看就和我平時的風格不同。對了。這不是我寫的,是我家高大英俊,寫一手好代碼,炒一手好菜的男神架構師老公的大作,曾發表於技術公號,經本人授權轉載,如有技術
Spring IOC實現原理
spring ioc實現原理一、IOC 容器:最主要是完成了完成對象的創建和依賴的管理註入等等。 所謂控制反轉,就是把原先我們代碼裏面需要實現的對象創建、依賴的代碼,反轉給容器來幫忙實現。那麽必然的我們需要創建一個容器,同時需要一種描述來讓容器知道需要創建的對象與對象的關系。這個描述最具體表現就是我們可配置
就是要你懂Java中volatile關鍵字實現原理
stub string home 技術分享 訪問速度 get 地址傳遞 code 緩沖 原文地址http://www.cnblogs.com/xrq730/p/7048693.html,轉載請註明出處,謝謝 前言 我們知道volatile關鍵字的作用是保證變量在多線程之