最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

深入理解Spring AOP

 更新時間:2017年01月07日 19:19:14   作者:五月的倉頡  
這篇文章主要介紹了深入理解Spring AOP,詳細(xì)的介紹了spring aop的具體實現(xiàn)與理論

一.前言

在以前的項目中,很少去關(guān)注spring aop的具體實現(xiàn)與理論,只是簡單了解了一下什么是aop具體怎么用,看到了一篇博文寫得還不錯,就來學(xué)習(xí)一下。

AOP

AOP(Aspect Oriented Programming),即面向切面編程,可以說是OOP(Object Oriented Programming,面向?qū)ο缶幊蹋┑难a(bǔ)充和完善。OOP引入封裝、繼承、多態(tài)等概念來建立一種對象層次結(jié)構(gòu),用于模擬公共行為的一個集合。不過OOP允許開發(fā)者定義縱向的關(guān)系,但并不適合定義橫向的關(guān)系,例如日志功能。日志代碼往往橫向地散布在所有對象層次中,而與它對應(yīng)的對象的核心功能毫無關(guān)系對于其他類型的代碼,如安全性、異常處理和透明的持續(xù)性也都是如此,這種散布在各處的無關(guān)的代碼被稱為橫切(cross cutting),在OOP設(shè)計中,它導(dǎo)致了大量代碼的重復(fù),而不利于各個模塊的重用。

AOP技術(shù)恰恰相反,它利用一種稱為"橫切"的技術(shù),剖解開封裝的對象內(nèi)部,并將那些影響了多個類的公共行為封裝到一個可重用模塊,并將其命名為"Aspect",即切面。所謂"切面",簡單說就是那些與業(yè)務(wù)無關(guān),卻為業(yè)務(wù)模塊所共同調(diào)用的邏輯或責(zé)任封裝起來,便于減少系統(tǒng)的重復(fù)代碼,降低模塊之間的耦合度,并有利于未來的可操作性和可維護(hù)性。

使用"橫切"技術(shù),AOP把軟件系統(tǒng)分為兩個部分:核心關(guān)注點和橫切關(guān)注點。業(yè)務(wù)處理的主要流程是核心關(guān)注點,與之關(guān)系不大的部分是橫切關(guān)注點。橫切關(guān)注點的一個特點是,他們經(jīng)常發(fā)生在核心關(guān)注點的多處,而各處基本相似,比如權(quán)限認(rèn)證、日志、事物。AOP的作用在于分離系統(tǒng)中的各種關(guān)注點,將核心關(guān)注點和橫切關(guān)注點分離開來。

AOP核心概念

1、橫切關(guān)注點

對哪些方法進(jìn)行攔截,攔截后怎么處理,這些關(guān)注點稱之為橫切關(guān)注點

2、切面(aspect)

類是對物體特征的抽象,切面就是對橫切關(guān)注點的抽象

3、連接點(joinpoint)

被攔截到的點,因為Spring只支持方法類型的連接點,所以在Spring中連接點指的就是被攔截到的方法,實際上連接點還可以是字段或者構(gòu)造器

4、切入點(pointcut)

對連接點進(jìn)行攔截的定義

5、通知(advice)

所謂通知指的就是指攔截到連接點之后要執(zhí)行的代碼,通知分為前置、后置、異常、最終、環(huán)繞通知五類

6、目標(biāo)對象

代理的目標(biāo)對象

7、織入(weave)

將切面應(yīng)用到目標(biāo)對象并導(dǎo)致代理對象創(chuàng)建的過程

8、引入(introduction)

在不修改代碼的前提下,引入可以在運行期為類動態(tài)地添加一些方法或字段

Spring對AOP的支持

Spring中AOP代理由Spring的IOC容器負(fù)責(zé)生成、管理,其依賴關(guān)系也由IOC容器負(fù)責(zé)管理。因此,AOP代理可以直接使用容器中的其它bean實例作為目標(biāo),這種關(guān)系可由IOC容器的依賴注入提供。Spring創(chuàng)建代理的規(guī)則為:

1、默認(rèn)使用Java動態(tài)代理來創(chuàng)建AOP代理,這樣就可以為任何接口實例創(chuàng)建代理了

2、當(dāng)需要代理的類不是代理接口的時候,Spring會切換為使用CGLIB代理,也可強(qiáng)制使用CGLIB

AOP編程其實是很簡單的事情,縱觀AOP編程,程序員只需要參與三個部分:

1、定義普通業(yè)務(wù)組件

2、定義切入點,一個切入點可能橫切多個業(yè)務(wù)組件

3、定義增強(qiáng)處理,增強(qiáng)處理就是在AOP框架為普通業(yè)務(wù)組件織入的處理動作

所以進(jìn)行AOP編程的關(guān)鍵就是定義切入點和定義增強(qiáng)處理,一旦定義了合適的切入點和增強(qiáng)處理,AOP框架將自動生成AOP代理,即:代理對象的方法=增強(qiáng)處理+被代理對象的方法。

下面給出一個Spring AOP的.xml文件模板,名字叫做aop.xml,之后的內(nèi)容都在aop.xml上進(jìn)行擴(kuò)展:

<?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簡單實現(xiàn) 

注意一下,在講解之前,說明一點:使用Spring AOP,要成功運行起代碼,只用Spring提供給開發(fā)者的jar包是不夠的,請額外上網(wǎng)下載兩個jar包:

1、aopalliance.jar

2、aspectjweaver.jar

開始講解用Spring AOP的XML實現(xiàn)方式,先定義一個接口:

public interface HelloWorld
{
 void printHelloWorld();
 void doPrint();
} 

定義兩個接口實現(xiàn)類:

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 ;
 }
} 

橫切關(guān)注點,這里是打印時間:

public class TimeHandler
{
 public void printTime()
 {
  System.out.println("CurrentTime = " + System.currentTimeMillis());
 }
} 

有這三個類就可以實現(xiàn)一個簡單的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函數(shù)調(diào)用一下:

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();
} 

運行結(jié)果為:

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接口的兩個實現(xiàn)類的所有方法都加上了代理,代理內(nèi)容就是打印時間

基于Spring的AOP使用其他細(xì)節(jié)

1、增加一個橫切關(guān)注點,打印日志,Java類為:

public class LogHandler
{
 public void LogBefore()
 {
  System.out.println("Log before method");
 }
 
 public void LogAfter()
 {
  System.out.println("Log after method");
 }
} 

<?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> 

測試類不變,打印結(jié)果為:

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屬性的數(shù)字就是橫切關(guān)注點的順序

(2)把logHandler定義在timeHandler前面,Spring默認(rèn)以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、強(qiáng)制使用CGLIB生成代理

前面說過Spring使用動態(tài)代理或是CGLIB生成代理是有規(guī)則的,高版本的Spring會自動選擇是使用動態(tài)代理還是CGLIB生成代理內(nèi)容,當(dāng)然我們也可以強(qiáng)制使用CGLIB生成代理,那就是<aop:config>里面有一個"proxy-target-class"屬性,這個屬性值如果被設(shè)置為true,那么基于類的代理將起作用,如果proxy-target-class被設(shè)置為false或者這個屬性被省略,那么基于接口的代理將起作用

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 如何把Java程序窗口在屏幕中間顯示

    如何把Java程序窗口在屏幕中間顯示

    大家在日常Java開發(fā)中,可能會需要把程序窗口定位在屏幕中間,那該如何操作呢,下面來一起看看。
    2016-08-08
  • java.util.NoSuchElementException原因及兩種解決方法

    java.util.NoSuchElementException原因及兩種解決方法

    本文主要介紹了java.util.NoSuchElementException原因及兩種解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • 基于spring三方包類注入容器的四種方式小結(jié)

    基于spring三方包類注入容器的四種方式小結(jié)

    這篇文章主要介紹了基于spring三方包類注入容器的四種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 深度剖析Java中的內(nèi)存原型及工作原理

    深度剖析Java中的內(nèi)存原型及工作原理

    這篇文章主要介紹了深度剖析Java中的內(nèi)存原型及工作原理,本文講解了java虛擬機(jī)內(nèi)存原型、常量池、Java內(nèi)存分配中的棧、Java內(nèi)存分配中的堆等內(nèi)容,需要的朋友可以參考下
    2015-01-01
  • Java mysql特殊形式的查詢語句詳解

    Java mysql特殊形式的查詢語句詳解

    這篇文章主要介紹了Java mysql特殊形式的查詢,包括子查詢和聯(lián)合查詢、自身連接查詢問題,本文通過sql語句給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-02-02
  • Spring?Boot條件注解之@ConditionalOnProperty完全解析

    Spring?Boot條件注解之@ConditionalOnProperty完全解析

    這篇文章主要介紹了SpringBoot中的@ConditionalOnProperty注解,通過配置文件屬性值控制Bean或配置類的加載,實現(xiàn)功能開關(guān)和環(huán)境配置,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-02-02
  • Springboot處理配置CORS跨域請求時碰到的坑

    Springboot處理配置CORS跨域請求時碰到的坑

    本篇文章介紹了我在開發(fā)過程中遇到的一個問題,以及解決該問題的過程及思路,通讀本篇對大家的學(xué)習(xí)或工作具有一定的價值,需要的朋友可以參考下
    2021-09-09
  • Springboot整合Activiti操作詳解

    Springboot整合Activiti操作詳解

    這篇文章主要給大家詳細(xì)介紹了Springboot整合Activiti的操作流程,文中流程步驟和代碼示例介紹的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下
    2023-07-07
  • springBoot集成shiro實現(xiàn)權(quán)限刷新

    springBoot集成shiro實現(xiàn)權(quán)限刷新

    在SpringBoot項目中集成Shiro進(jìn)行權(quán)限管理,包括基礎(chǔ)配置引入依賴、創(chuàng)建Shiro配置類以及用戶認(rèn)證與授權(quán)實現(xiàn),具有一定的參考價值,感興趣的可以了解一下
    2024-11-11
  • Java參數(shù)校驗@Validated、@Valid介紹及使用詳解

    Java參數(shù)校驗@Validated、@Valid介紹及使用詳解

    Javax.validation是?spring?集成自帶的一個參數(shù)校驗接口,可通過添加注解來設(shè)置校驗條件,這篇文章主要介紹了Java參數(shù)校驗@Validated、@Valid介紹及使用詳解,需要的朋友可以參考下
    2024-08-08

最新評論

东兰县| 如东县| 古浪县| 加查县| 宾阳县| 晋州市| 修水县| 嘉义市| 张家港市| 西华县| 乌拉特后旗| 乐清市| 城步| 台东市| 永康市| 黑水县| 定日县| 泾阳县| 天津市| 鄂尔多斯市| 磴口县| 邳州市| 金乡县| 陆丰市| 玉溪市| 延边| 大竹县| 宜章县| 新宁县| 全州县| 乡宁县| 南安市| 依安县| 邯郸市| 循化| 曲靖市| 原阳县| 宜兴市| 新化县| 江源县| 雷山县|