Spring中怎么利用ProxyFactoryBean創(chuàng)建AOP代理

Spring中怎么利用ProxyFactoryBean創(chuàng)建AOP代理,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

創(chuàng)新互聯(lián)主要從事成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)岳陽(yáng),10余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專(zhuān)業(yè),歡迎來(lái)電咨詢(xún)建站服務(wù):18982081108

Spring 通知類(lèi)型

通過(guò)前面的學(xué)習(xí)可以知道,通知(Advice)其實(shí)就是對(duì)目標(biāo)切入點(diǎn)進(jìn)行增強(qiáng)的內(nèi)容,Spring AOP 為通知(Advice)提供了 org.aopalliance.aop.Advice 接口。

Spring 通知按照在目標(biāo)類(lèi)方法的連接點(diǎn)位置,可以分為以下五種類(lèi)型,如表 1 所示。

表 1 Spring 通知的 5 種類(lèi)型

org.springframework.aop.MethodBeforeAdvice(前置通知)      在方法之前自動(dòng)執(zhí)行的通知稱(chēng)為前置通知,可以應(yīng)用于權(quán)限管理等功能。              org.springframework.aop.AfterReturningAdvice(后置通知)      在方法之后自動(dòng)執(zhí)行的通知稱(chēng)為后置通知,可以應(yīng)用于關(guān)閉流、上傳文件、刪除臨時(shí)文件等功能。              org.aopalliance.intercept.MethodInterceptor(環(huán)繞通知)      在方法前后自動(dòng)執(zhí)行的通知稱(chēng)為環(huán)繞通知,可以應(yīng)用于日志、事務(wù)管理等功能。              org.springframework.aop.ThrowsAdvice(異常通知)      在方法拋出異常時(shí)自動(dòng)執(zhí)行的通知稱(chēng)為異常通知,可以應(yīng)用于處理異常記錄日志等功能。              org.springframework.aop.IntroductionInterceptor(引介通知)      在目標(biāo)類(lèi)中添加一些新的方法和屬性,可以應(yīng)用于修改舊版本程序(增強(qiáng)類(lèi))。

聲明式 Spring AOP

Spring 創(chuàng)建一個(gè) AOP 代理的基本方法是使用 org.springframework.aop.framework.ProxyFactoryBean,這個(gè)類(lèi)對(duì)應(yīng)的切入點(diǎn)和通知提供了完整的控制能力,并可以生成指定的內(nèi)容。

ProxyFactoryBean 類(lèi)中的常用可配置屬性如表 2 所示。

表 2 ProxyFactoryBean 的常用屬性

target      代理的目標(biāo)對(duì)象              proxyInterfaces      代理要實(shí)現(xiàn)的接口,如果有多個(gè)接口,則可以使用以下格式賦值:      <list>        <value ></value>        ...      </list>              proxyTargetClass      是否對(duì)類(lèi)代理而不是接口,設(shè)置為 true 時(shí),使用 CGLIB 代理              interceptorNames      需要植入目標(biāo)的 Advice              singleton      返回的代理是否為單例,默認(rèn)為 true(返回單實(shí)例)              optimize      當(dāng)設(shè)置為 true 時(shí),強(qiáng)制使用 CGLIB

在 Spring 通知中,環(huán)繞通知是一個(gè)非常典型的應(yīng)用。下面通過(guò)環(huán)繞通知的案例演示 Spring 創(chuàng)建 AOP 代理的過(guò)程。

1. 導(dǎo)入 JAR 包

在核心 JAR 包的基礎(chǔ)上,再向 springDemo03 項(xiàng)目的 lib 目錄中導(dǎo)入 AOP 的 JAR 包,具體如下。

spring-aop-3.2.13.RELEASE.jar:是 Spring 為 AOP 提供的實(shí)現(xiàn),在 Spring 的包中已經(jīng)提供。  com.springsource.org.aopalliance-1.0.0.jar:是 AOP 提供的規(guī)范,可以在 Spring 的官網(wǎng)網(wǎng)址https://repo.spring.io/webapp/#/search/quick/ 中進(jìn)行搜索并下載。

2. 創(chuàng)建切面類(lèi) MyAspect

在 src 目錄下創(chuàng)建一個(gè)名為 com.mengma.factorybean 的包,在該包下創(chuàng)建切面類(lèi) MyAspect,如下所示。

package com.mengma.factorybean;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;//需要實(shí)現(xiàn)接口,確定哪個(gè)通知,及告訴Spring應(yīng)該執(zhí)行哪個(gè)方法public class MyAspect implements MethodInterceptor {public Object invoke(MethodInvocation mi) throws Throwable {System.out.println("方法執(zhí)行之前");// 執(zhí)行目標(biāo)方法Object obj = mi.proceed();System.out.println("方法執(zhí)行之后");return obj;}}

上述代碼中,MyAspect 類(lèi)實(shí)現(xiàn)了 MethodInterceptor 接口,并實(shí)現(xiàn)了接口的 invoke() 方法。MethodInterceptor 接口是 Spring AOP 的 JAR 包提供的,而 invoke() 方法用于確定目標(biāo)方法 mi,并告訴 Spring 要在目標(biāo)方法前后執(zhí)行哪些方法,這里為了演示效果在目標(biāo)方法前后分別向控制臺(tái)輸出了相應(yīng)語(yǔ)句。

3. 創(chuàng)建 Spring 配置文件

在 com.mengma.factorybean 包下創(chuàng)建配置文件 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"  xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd">  <!--目標(biāo)類(lèi) -->  <bean id="customerDao" class="com.mengma.dao.CustomerDaoImpl" />  <!-- 通知 advice -->  <bean id="myAspect" class="com.mengma.factorybean.MyAspect" />  <!--生成代理對(duì)象 -->  <bean id="customerDaoProxy"   class="org.springframework.aop.framework.ProxyFactoryBean">   <!--代理實(shí)現(xiàn)的接口 -->    <property name="proxyInterfaces" value="com.mengma.dao.CustomerDao" />    <!--代理的目標(biāo)對(duì)象 -->    <property name="target" ref="customerDao" />    <!--用通知增強(qiáng)目標(biāo) -->    <property name="interceptorNames" value="myAspect" />    <!-- 如何生成代理,true:使用cglib; false :使用jdk動(dòng)態(tài)代理 -->    <property name="proxyTargetClass" value="true" />  </bean></beans>

名稱(chēng)說(shuō)明
屬性名稱(chēng)描 述

上述代碼中,首先配置目標(biāo)類(lèi)和通知,然后使用 ProxyFactoryBean 類(lèi)生成代理對(duì)象;第 14 行代碼配置了代理實(shí)現(xiàn)的接口;第 16 行代碼配置了代理的目標(biāo)對(duì)象;第 18 行代碼配置了需要植入目標(biāo)的通知;當(dāng)?shù)?20 行代碼中的 value 屬性值為 true 時(shí),表示使用 CGLIB 代理,屬性值為 false 時(shí),表示使用 JDK 動(dòng)態(tài)代理。

4. 創(chuàng)建測(cè)試類(lèi)

在 com.mengma.factorybean 包下創(chuàng)建一個(gè)名為 FactoryBeanTest 的測(cè)試類(lèi),編輯后如下所示。

package com.mengma.factorybean;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.mengma.dao.CustomerDao;public class FactoryBeanTest {@Testpublic void test() {String xmlPath = "com/mengma/factorybean/applicationContext.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);CustomerDao customerDao = (CustomerDao) applicationContext.getBean("customerDaoProxy");customerDao.add();customerDao.update();customerDao.delete();customerDao.find();}}

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

當(dāng)前名稱(chēng):Spring中怎么利用ProxyFactoryBean創(chuàng)建AOP代理
本文來(lái)源:http://muchs.cn/article40/ghseeo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè)、定制開(kāi)發(fā)、品牌網(wǎng)站制作、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化