欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Java中Spring框架之AOP如何配置

這篇文章主要介紹了Java中Spring框架之AOP如何配置,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

曲松網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、自適應(yīng)網(wǎng)站建設(shè)等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)于2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)

  什么是AOP

AOP(Aspect Oriented Programming)面向切面編程,是OOP(面向?qū)ο缶幊蹋┑闹匾a充,面向?qū)ο笫荍ava編程的基礎(chǔ),以封裝、繼承、多態(tài)建立了對象間的層次關(guān)系,關(guān)注的是每個對象的具體實現(xiàn)。面向?qū)ο笤试S我們開發(fā)縱向的關(guān)系。

AOP關(guān)注橫向關(guān)系,也就是說類和類之間不需要特定的關(guān)系,它能夠?qū)⒛承┩ㄓ玫牟僮鳎ㄈ纾喝罩咎幚怼踩炞C、事務(wù)處理、異常處理、性能統(tǒng)計等)插入到這些無關(guān)的類中,從而可以讓每個類只關(guān)注自己的核心邏輯,不必處理這些通用的操作,這個過程就是橫切,而這些通用的操作就是切面Aspect。

簡單來說:AOP能把和類的核心業(yè)務(wù)無關(guān),多個類又共同需要的邏輯代碼封裝起來,當(dāng)對象需要時自動調(diào)用,這樣就減少了類中的重復(fù)代碼,降低了類之間的耦合性,提高了程序的維護性。

  AOP術(shù)語

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

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

2、切面(aspect)

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

3、連接點(joinpoint)

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

4、切入點(pointcut)

對連接點進行攔截的定義

5、通知(advice)

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

6、目標(biāo)對象

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

7、織入(weave)

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

8、引入(introduction)

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

  AOP應(yīng)用場景

1)日志 ,方法執(zhí)行開始和完成以及出現(xiàn)異常都可以用日志記錄

2)緩存 ,第一次調(diào)用查詢數(shù)據(jù)庫,將查詢結(jié)果放入內(nèi)存對象, 第二次調(diào)用, 直接從內(nèi)存對象返回,不需要查詢數(shù)據(jù)庫

3)事務(wù) ,調(diào)用方法前開啟事務(wù), 調(diào)用方法后提交關(guān)閉事務(wù)

等等

  AOP的配置

1)導(dǎo)入Maven依賴

<dependencies>

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.12</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>4.3.14.Release</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>4.3.14.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-beans</artifactId>

<version>4.3.14.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context-support</artifactId>

<version>4.3.14.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-expression</artifactId>

<version>4.3.14.RELEASE</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-aop</artifactId>

<version>4.3.14.RELEASE</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjrt</artifactId>

<version>1.8.13</version>

</dependency>

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->

<dependency>

<groupId>org.aspectj</groupId>

<artifactId>aspectjweaver</artifactId>

<version>1.8.13</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-test</artifactId>

<version>4.3.14.RELEASE</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.1.2</version>

</dependency>

<dependency>

<groupId>log4j</groupId>

<artifactId>log4j</artifactId>

<version>1.2.17</version>

</dependency>

</dependencies>

編寫Service接口和實現(xiàn)類,這里只完成模擬操作

public interface AdminService {

void saveAdmin();

void updateAdmin();

void deleteAdmin();

void selectAdmin();

}

public class AdminServiceImpl implements AdminService {

@Override

public void saveAdmin() {

System.out.println("Invoke saveAdmin");

}

@Override

public void updateAdmin() {

System.out.println("Invoke updateAdmin");

}

@Override

public void deleteAdmin() {

System.out.println("Invoke deleteAdmin");

}

@Override

public void selectAdmin() {

System.out.println("Invoke selectAdmin");

}

}

3)編寫自定義增強類,該類的方法對應(yīng)AOP的5種增強通知,分別是:

1)前置通知before :方法調(diào)用前執(zhí)行

2)后置通知after-returning:方法調(diào)用后執(zhí)行,出現(xiàn)異常不執(zhí)行

3)后置通知after:方法調(diào)用后執(zhí)行,出現(xiàn)異常也執(zhí)行

4)異常通知after-throwing:出現(xiàn)異常執(zhí)行

5)環(huán)繞通知around:方法調(diào)用前后執(zhí)行

/**

* 自定義增強類

*/

public class MyAdvise {

public void before() throws Throwable {

System.out.println("這是before");

}

public void afterReturning(){

System.out.println("這是afterReturning");

}

public void after(){

System.out.println("這是after");

}

public void afterThrowing() throws Throwable {

System.out.println("這是afterThrowing");

}

public Object around(ProceedingJoinPoint point) throws Throwable {

System.out.println("這是around -- 前置執(zhí)行");

Object obj = point.proceed();

System.out.println("這是around -- 后置執(zhí)行");

return obj;

}

}

4)   Spring配置文件

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

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd">

<bean id="userService" class="com.qianfeng.springaop.UserServiceImpl"/>

<bean id="adminService" class="com.qianfeng.springaop.AdminServiceImpl"/>

<bean id="myAdvise" class="com.qianfeng.springaop.MyAdvise"/>

<aop:config>

<!--配置切入點

execution中第一個*代表任意返回值類型,后面是包名,第二個*代表類名的開頭,第三個*代表任意方法,(..)代表任意方法參數(shù)

-->

<aop:pointcut id="c" expression="execution(* com.qianfeng.springaop.*ServiceImpl.*(..))"/>

<!--配置方面-->

<aop:aspect ref="myAdvise">

<aop:before method="before" pointcut-ref="c"/>

<aop:after method="after" pointcut-ref="c"/>

<aop:after-returning method="afterReturning" pointcut-ref="c"/>

<aop:after-throwing method="afterThrowing" pointcut-ref="c"/>

<aop:around method="around" pointcut-ref="c"/>

</aop:aspect>

</aop:config>

</beans>

5)運行測試

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration("classpath:applicationContext-aop.xml")

public class TestAOP {

@Test

public void test(){

ApplicationContext app = new

ClassPathXmlApplicationContext("applicationContext-aop.xml");

AdminService as = (AdminService) app.getBean("adminService");

as.deleteAdmin();

}

}

可以看到執(zhí)行任何Service類的方法,都會調(diào)用MyAdvise中的通知方法

感謝你能夠認真閱讀完這篇文章,希望小編分享的“Java中Spring框架之AOP如何配置”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

分享題目:Java中Spring框架之AOP如何配置
文章源于:http://www.chinadenli.net/article16/joepgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)用戶體驗網(wǎng)站維護網(wǎng)站設(shè)計公司網(wǎng)站策劃網(wǎng)站收錄

廣告

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

網(wǎng)站優(yōu)化排名