欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springAOP的三種實(shí)現(xiàn)方式示例代碼

 更新時(shí)間:2020年07月20日 17:27:14   作者:易水寒的博客  
這篇文章主要介紹了springAOP的三種實(shí)現(xiàn)方式,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

這篇文章給大家介紹了springAOP的實(shí)現(xiàn)方式,三種分別是純XML方式,XML+注解,純注解方式。

Spring 實(shí)現(xiàn)AOP思想使⽤的是動(dòng)態(tài)代理技術(shù)
默認(rèn)情況下, Spring會(huì)根據(jù)被代理對(duì)象是否實(shí)現(xiàn)接⼝來選擇使⽤JDK還是CGLIB。當(dāng)被代理對(duì)象沒有實(shí)現(xiàn)

任何接⼝時(shí), Spring會(huì)選擇CGLIB。當(dāng)被代理對(duì)象實(shí)現(xiàn)了接⼝, Spring會(huì)選擇JDK官⽅的代理技術(shù),不過

我們可以通過配置的⽅式,讓Spring強(qiáng)制使⽤CGLIB。

接下來我們開始實(shí)現(xiàn)aop,
需求是:橫切邏輯代碼是打印⽇志,希望把打印⽇志的邏輯織⼊到⽬標(biāo)⽅法的特定位置(service層transfer⽅法)

純XML方式

引入aop相關(guān)的jar包

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>5.1.12.RELEASE</version>
</dependency>

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.9.4</version>
</dependency>

TransferServiceImpl.java文件:

package com.lagou.edu.service.impl;
import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.TransferService;
import com.lagou.edu.utils.ConnectionUtils;
import com.lagou.edu.utils.TransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.ImportResource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
/**
 * @author 應(yīng)癲
 */
@Service("transferService")
public class TransferServiceImpl implements TransferService {
  // 最佳狀態(tài)
  // @Autowired 按照類型注入 ,如果按照類型無法唯一鎖定對(duì)象,可以結(jié)合@Qualifier指定具體的id
  @Autowired
  @Qualifier("accountDao")
  private AccountDao accountDao;

  @Override
  public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
    /*try{
      // 開啟事務(wù)(關(guān)閉事務(wù)的自動(dòng)提交)
      TransactionManager.getInstance().beginTransaction();*/
      System.out.println("執(zhí)行轉(zhuǎn)賬業(yè)務(wù)邏輯");
      Account from = accountDao.queryAccountByCardNo(fromCardNo);
      Account to = accountDao.queryAccountByCardNo(toCardNo);
      from.setMoney(from.getMoney()-money);
      to.setMoney(to.getMoney()+money);
      accountDao.updateAccountByCardNo(to);
      //int c = 1/0;
      accountDao.updateAccountByCardNo(from);
  }
}

打印日志Util:

package com.lagou.edu.utils;

/**
 * @author 應(yīng)癲
 */

public class LogUtils {

  /**
   * 業(yè)務(wù)邏輯開始之前執(zhí)行
   */
  
  public void beforeMethod(JoinPoint joinPoint) {
     Object[] args = joinPoint.getArgs();
    for (int i = 0; i < args.length; i++) {
      Object arg = args[i];
      System.out.println(arg);
    }
    System.out.println("業(yè)務(wù)邏輯開始執(zhí)行之前執(zhí)行.......");
  }


  /**
   * 業(yè)務(wù)邏輯結(jié)束時(shí)執(zhí)行(無論異常與否)
   */

  public void afterMethod() {
    System.out.println("業(yè)務(wù)邏輯結(jié)束時(shí)執(zhí)行,無論異常與否都執(zhí)行.......");
  }

  /**
   * 異常時(shí)時(shí)執(zhí)行
   */
  public void exceptionMethod() {
    System.out.println("異常時(shí)執(zhí)行.......");
  }

  /**
   * 業(yè)務(wù)邏輯正常時(shí)執(zhí)行
   */

  public void successMethod(Object retVal) {
    System.out.println("業(yè)務(wù)邏輯正常時(shí)執(zhí)行.......");
  }

}

public Object arroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    System.out.println("環(huán)繞通知中的beforemethod....");

    Object result = null;
    try{
      // 控制原有業(yè)務(wù)邏輯是否執(zhí)行
      // result = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
    }catch(Exception e) {
      System.out.println("環(huán)繞通知中的exceptionmethod....");
    }finally {
      System.out.println("環(huán)繞通知中的after method....");
    }

    return result;
  }

applicationContext.xml

<!--進(jìn)行aop相關(guān)的xml配置,配置aop的過程其實(shí)就是把a(bǔ)op相關(guān)術(shù)語落地-->
  <!--橫切邏輯bean-->
<bean id="logUtils" class="com.lagou.edu.utils.LogUtils"></bean>
  <!--使用config標(biāo)簽表明開始aop配置,在內(nèi)部配置切面aspect-->

  <!--aspect  =  切入點(diǎn)(鎖定方法) + 方位點(diǎn)(鎖定方法中的特殊時(shí)機(jī))+ 橫切邏輯 -->
  <aop:config>
    <aop:aspect id="logAspect" ref="logUtils">

      <!--切入點(diǎn)鎖定我們感興趣的方法,使用aspectj語法表達(dá)式-->
      <!--..參數(shù)中的兩個(gè)點(diǎn)表示可以有參數(shù),也可以沒有參數(shù),有的話也可以是任意類型,參數(shù)中的 *表示參數(shù)可以是任意類型,但必須有參數(shù)。 -->
      <!--包名中的..兩個(gè)點(diǎn)表示中間可以是任意層-->
      <!--<aop:pointcut id="pt1" expression="execution(* *..*.*(..))"/>-->
     <aop:pointcut id="pt1" expression="execution(public void com.lagou.edu.service.impl.TransferServiceImpl.transfer(java.lang.String,java.lang.String,int))"/>

<!--      <aop:pointcut id="pt1" expression="execution(* com.lagou.edu.service.impl.TransferServiceImpl.*(..))"/>
-->
      <!--方位信息,pointcut-ref關(guān)聯(lián)切入點(diǎn)-->
      <!--aop:before前置通知/增強(qiáng)-->
      <aop:before method="beforeMethod" pointcut-ref="pt1"/>
      <!--aop:after,最終通知,無論如何都執(zhí)行-->
      <!--aop:after-returnning,正常執(zhí)行通知,retValue是接受方法的返回值的-->
      <aop:after-returning method="successMethod" returning="retValue"/>
      <!--aop:after-throwing,異常通知-->

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

    </aop:aspect>
  </aop:config>-->

測(cè)試:

 /**
   * 測(cè)試xml aop
   */
  @Test
  public void testXmlAop() throws Exception {
    ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    TransferService transferService = applicationContext.getBean(TransferService.class);
    transferService.transfer("6029621011000","6029621011001",100);
  }

環(huán)繞通知不和前置及后置通知一起使用,因?yàn)榄h(huán)繞通知可以實(shí)現(xiàn)前置和后置的功能,并且可以控制原有業(yè)務(wù)邏輯是否執(zhí)行,非常強(qiáng)大。

XML+注解方式

將上面純XML方式改為注解方式
將applicationContext.xml中的內(nèi)容取掉,改為類中添加注解:

package com.lagou.edu.utils;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author 應(yīng)癲
 */
@Component
@Aspect
public class LogUtils {


  @Pointcut("execution(* com.lagou.edu.service.impl.TransferServiceImpl.*(..))")
  public void pt1(){

  }


  /**
   * 業(yè)務(wù)邏輯開始之前執(zhí)行
   */
  @Before("pt1()")
  public void beforeMethod(JoinPoint joinPoint) {
    Object[] args = joinPoint.getArgs();
    for (int i = 0; i < args.length; i++) {
      Object arg = args[i];
      System.out.println(arg);
    }
    System.out.println("業(yè)務(wù)邏輯開始執(zhí)行之前執(zhí)行.......");
  }


  /**
   * 業(yè)務(wù)邏輯結(jié)束時(shí)執(zhí)行(無論異常與否)
   */
  @After("pt1()")
  public void afterMethod() {
    System.out.println("業(yè)務(wù)邏輯結(jié)束時(shí)執(zhí)行,無論異常與否都執(zhí)行.......");
  }


  /**
   * 異常時(shí)時(shí)執(zhí)行
   */
  @AfterThrowing("pt1()")
  public void exceptionMethod() {
    System.out.println("異常時(shí)執(zhí)行.......");
  }


  /**
   * 業(yè)務(wù)邏輯正常時(shí)執(zhí)行
   */
  @AfterReturning(value = "pt1()",returning = "retVal")
  public void successMethod(Object retVal) {
    System.out.println("業(yè)務(wù)邏輯正常時(shí)執(zhí)行.......");
  }


  /**
   * 環(huán)繞通知
   *
   */
  /*@Around("pt1()")*/
  public Object arroundMethod(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    System.out.println("環(huán)繞通知中的beforemethod....");

    Object result = null;
    try{
      // 控制原有業(yè)務(wù)邏輯是否執(zhí)行
      // result = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
    }catch(Exception e) {
      System.out.println("環(huán)繞通知中的exceptionmethod....");
    }finally {
      System.out.println("環(huán)繞通知中的after method....");
    }

    return result;
  }

}

在application.xml中配置注解驅(qū)動(dòng):

 <!--開啟aop注解驅(qū)動(dòng)
    proxy-target-class:true強(qiáng)制使用cglib

  -->
  <aop:aspectj-autoproxy/>

純注解模式

我們只需要替換掉xml+注解模式中的注解驅(qū)動(dòng)的部分即可,

 <!--開啟aop注解驅(qū)動(dòng)
    proxy-target-class:true強(qiáng)制使用cglib

  -->
  <aop:aspectj-autoproxy/>

改為 @EnableAspectJAutoProxy //開啟spring對(duì)注解AOP的⽀持,在項(xiàng)目中添加到任意個(gè)配置類上即可。

到此這篇關(guān)于springAOP的三種實(shí)現(xiàn)方式的文章就介紹到這了,更多相關(guān)springAOP實(shí)現(xiàn)方式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java非遞歸實(shí)現(xiàn)之二叉樹的前中后序遍歷詳解

    java非遞歸實(shí)現(xiàn)之二叉樹的前中后序遍歷詳解

    樹的遍歷順序大體分為三種:前序遍歷(先根遍歷、先序遍歷),中序遍歷(中根遍歷),后序遍歷(后根遍歷),本文將給大家詳細(xì)的介紹,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值
    2021-09-09
  • java中封裝JDBC工具類的實(shí)例分析

    java中封裝JDBC工具類的實(shí)例分析

    在本篇內(nèi)容里小編給各位分享了一篇關(guān)于java中封裝JDBC工具類的實(shí)例分析,對(duì)此有興趣的朋友們可以學(xué)習(xí)下。
    2021-03-03
  • SpringBoot+Docker+IDEA實(shí)現(xiàn)一鍵構(gòu)建+推送、運(yùn)行、同鏡像多容器啟動(dòng)

    SpringBoot+Docker+IDEA實(shí)現(xiàn)一鍵構(gòu)建+推送、運(yùn)行、同鏡像多容器啟動(dòng)

    這篇文章主要介紹了SpringBoot+Docker+IDEA實(shí)現(xiàn)一鍵構(gòu)建+推送、運(yùn)行、同鏡像多容器啟動(dòng),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • mybatis實(shí)現(xiàn)獲取入?yún)⑹荓ist和Map的取值

    mybatis實(shí)現(xiàn)獲取入?yún)⑹荓ist和Map的取值

    這篇文章主要介紹了mybatis實(shí)現(xiàn)獲取入?yún)⑹荓ist和Map的取值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • IDEA生成標(biāo)準(zhǔn)JavaBean的幾種方法總結(jié)

    IDEA生成標(biāo)準(zhǔn)JavaBean的幾種方法總結(jié)

    標(biāo)準(zhǔn)javaBean是定義一個(gè)類的標(biāo)準(zhǔn)結(jié)構(gòu),下面這篇文章主要給大家總結(jié)介紹了關(guān)于IDEA生成標(biāo)準(zhǔn)JavaBean的幾種方法,文中通過圖文以及代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • java中stream去重的幾種方式舉例

    java中stream去重的幾種方式舉例

    Stream流是數(shù)據(jù)渠道,用于操作數(shù)據(jù)源(集合、數(shù)組等)所生成的元素序列,這篇文章主要給大家介紹了關(guān)于java中stream去重的幾種方式,需要的朋友可以參考下
    2023-07-07
  • Java鎖的升級(jí)策略 偏向鎖 輕量級(jí)鎖 重量級(jí)鎖

    Java鎖的升級(jí)策略 偏向鎖 輕量級(jí)鎖 重量級(jí)鎖

    在本文中小編給的大家整理了關(guān)于Java鎖的升級(jí)策略 偏向鎖 輕量級(jí)鎖 重量級(jí)鎖的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們參考下。
    2019-06-06
  • 一文詳解SpringBoot中CommandLineRunner接口

    一文詳解SpringBoot中CommandLineRunner接口

    Spring Boot的CommandLineRunner接口是一個(gè)函數(shù)式接口,用于在Spring Boot應(yīng)用程序啟動(dòng)后執(zhí)行一些初始化操作,它提供了一個(gè)run方法,該方法在應(yīng)用程序啟動(dòng)后被調(diào)用,本文給大家詳細(xì)介紹了SpringBoot中CommandLineRunner接口,需要的朋友可以參考下
    2023-10-10
  • Java Gradle項(xiàng)目中的資源正確獲取方式

    Java Gradle項(xiàng)目中的資源正確獲取方式

    這篇文章主要介紹了Java Gradle項(xiàng)目中的資源正確獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • java編程多線程并發(fā)處理實(shí)例解析

    java編程多線程并發(fā)處理實(shí)例解析

    這篇文章主要介紹了java編程多線程并發(fā)處理實(shí)例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01

最新評(píng)論