溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

spring aop怎么用

發布時間:2021-08-09 11:34:18 來源:億速云 閱讀:158 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關spring aop怎么用的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

 AOP:【動態代理】

指在程序運行期間動態的將某段代碼切入到指定方法指定位置進行運行的編程方式;

1、導入aop模塊;Spring AOP:(spring-aspects)

2、定義一個業務邏輯類(MathCalculator);在業務邏輯運行的時候將日志進行打?。ǚ椒ㄖ?、方法運行結束、方法出現異常,xxx)

3、定義一個日志切面類(LogAspects):切面類里面的方法需要動態感知MathCalculator.div運行到哪里然后執行;

通知方法:

  • 前置通知(@Before):logStart:在目標方法(div)運行之前運行

  • 后置通知(@After):logEnd:在目標方法(div)運行結束之后運行(無論方法正常結束還是異常結束)

  • 返回通知(@AfterReturning):logReturn:在目標方法(div)正常返回之后運行

  • 異常通知(@AfterThrowing):logException:在目標方法(div)出現異常以后運行

  • 環繞通知(@Around):動態代理,手動推進目標方法運行(joinPoint.procced())

4、給切面類的目標方法標注何時何地運行(通知注解);

5、將切面類和業務邏輯類(目標方法所在類)都加入到容器中;

6、必須告訴Spring哪個類是切面類(給切面類上加一個注解:@Aspect)

[7]、給配置類中加 @EnableAspectJAutoProxy 【開啟基于注解的aop模式】
在Spring中很多的 @EnableXXX;

三步:

1)、將業務邏輯組件和切面類都加入到容器中;告訴Spring哪個是切面類(@Aspect)

2)、在切面類上的每一個通知方法上標注通知注解,告訴Spring何時何地運行(切入點表達式)

3)、開啟基于注解的aop模式;@EnableAspectJAutoProxy

package com.opk.bean;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

/* 
 * @Aspect: 告訴Spring當前類是一個切面類
 * wmy 13:03 2019/9/5
 * @Param 
 * @return 
 **/
@Aspect
public class LogAspects {

  //抽取公共的切入點表達式
  //1、本類引用
  //2、其他的切面引用
  @Pointcut("execution(public * com.opk.bean.*.*(..))")
  public void pointCut(){};

  //@Before在目標方法之前切入;切入點表達式(指定在哪個方法切入)
  @Before("pointCut()")
  public void logStart(JoinPoint joinPoint){
    Object[] args = joinPoint.getArgs();
    System.out.println(joinPoint.getSignature().getName()+":運行。。。@Before,參數列表是:"+ Arrays.asList(args));
  }
  @After("com.opk.bean.LogAspects.pointCut()")
  public void logEnd(JoinPoint joinPoint){
    System.out.println(""+joinPoint.getSignature().getName()+"結束。。。@After");
  }

  //JoinPoint一定要出現在參數的第一位
  @AfterReturning(value ="pointCut()",returning = "result")
  public void logReturn(JoinPoint joinPoint,Object result){
    System.out.println(""+joinPoint.getSignature().getName()+"正常返回。。。@AfterReturning:運行結果:{"+result+"}");
  }

  @AfterThrowing(value ="pointCut()",throwing = "ex")
  public void logException(JoinPoint joinPoint,Exception ex){
    System.out.println(""+joinPoint.getSignature().getName()+"異常。。。異常信息:{"+ex+"}");
  }
}
import org.springframework.stereotype.Component;

@Component
public class MathCalculator {
  public int dev(int i,int j)
  {
    System.out.println("MathCalculator......");
    return i/j;
  }
}
@EnableAspectJAutoProxy
@Configuration
public class AOPConfig {

  //業務邏輯類加入容器中
  @Bean("calculator")
  public MathCalculator calculator(){
    return new MathCalculator();
  }

  //切面類加入到容器中
  @Bean
  public LogAspects logAspects(){
    return new LogAspects();
  }
}
public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(AOPConfig.class);
    ctx.refresh();
    MathCalculator bean = (MathCalculator)ctx.getBean("calculator");
    bean.dev(10,3);

  }

運行結果:

spring aop怎么用

感謝各位的閱讀!關于“spring aop怎么用”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女