成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

使用SpringBoot怎么對SpringAOP進行集成

今天就跟大家聊聊有關使用SpringBoot怎么對Spring AOP進行集成,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)是由多位在大型網(wǎng)絡公司、廣告設計公司的優(yōu)秀設計人員和策劃人員組成的一個具有豐富經(jīng)驗的團隊,其中包括網(wǎng)站策劃、網(wǎng)頁美工、網(wǎng)站程序員、網(wǎng)頁設計師、平面廣告設計師、網(wǎng)絡營銷人員及形象策劃。承接:網(wǎng)站制作、成都做網(wǎng)站、網(wǎng)站改版、網(wǎng)頁設計制作、網(wǎng)站建設與維護、網(wǎng)絡推廣、數(shù)據(jù)庫開發(fā),以高性價比制作企業(yè)網(wǎng)站、行業(yè)門戶平臺等全方位的服務。

需要的jar包添加到工程里。新增Maven依賴如下:

<dependency> 
  <groupId>org.springframework.boot</groupId> 
  <artifactId>spring-boot-starter-aop</artifactId> 
</dependency> 

 接下來,我們進入正題。這里的涉及的通知類型有:前置通知、后置最終通知、后置返回通知、后置異常通知、環(huán)繞通知,下面我們就具體的來看一下怎么在SpringBoot中添加這些通知。

首先我們先創(chuàng)建一個Aspect切面類:

@Component 
@Aspect 
public class WebControllerAop { 
 
} 

指定切點:

//匹配com.zkn.learnspringboot.web.controller包及其子包下的所有類的所有方法 
@Pointcut("execution(* com.zkn.learnspringboot.web.controller..*.*(..))") 
public void executeService(){ 
 
} 

接著我們再創(chuàng)建一個Controller請求處理類:

package com.zkn.learnspringboot.web.controller; 
 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
 
/** 
 * Created by zkn on 2016/11/19. 
 */ 
@RestController 
@RequestMapping("/aop") 
public class AopTestController { 
 
} 

前置通知

配置前置通知:

/** 
 * 前置通知,方法調(diào)用前被調(diào)用 
 * @param joinPoint 
 */ 
@Before("executeService()") 
public void doBeforeAdvice(JoinPoint joinPoint){ 
  System.out.println("我是前置通知!!!"); 
  //獲取目標方法的參數(shù)信息 
  Object[] obj = joinPoint.getArgs(); 
  //AOP代理類的信息 
  joinPoint.getThis(); 
  //代理的目標對象 
  joinPoint.getTarget(); 
  //用的最多 通知的簽名 
  Signature signature = joinPoint.getSignature(); 
  //代理的是哪一個方法 
  System.out.println(signature.getName()); 
  //AOP代理類的名字 
  System.out.println(signature.getDeclaringTypeName()); 
  //AOP代理類的類(class)信息 
  signature.getDeclaringType(); 
  //獲取RequestAttributes 
  RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); 
  //從獲取RequestAttributes中獲取HttpServletRequest的信息 
  HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST); 
  //如果要獲取Session信息的話,可以這樣寫: 
  //HttpSession session = (HttpSession) requestAttributes.resolveReference(RequestAttributes.REFERENCE_SESSION); 
  Enumeration<String> enumeration = request.getParameterNames(); 
  Map<String,String> parameterMap = Maps.newHashMap(); 
  while (enumeration.hasMoreElements()){ 
    String parameter = enumeration.nextElement(); 
    parameterMap.put(parameter,request.getParameter(parameter)); 
  } 
  String str = JSON.toJSONString(parameterMap); 
  if(obj.length > 0) { 
    System.out.println("請求的參數(shù)信息為:"+str); 
  } 
} 

注意:這里用到了JoinPoint和RequestContextHolder。通過JoinPoint可以獲得通知的簽名信息,如目標方法名、目標方法參數(shù)信息等。通過RequestContextHolder來獲取請求信息,Session信息。

接下來我們在Controller類里添加一個請求處理方法來測試一下前置通知:

@RequestMapping("/testBeforeService.do") 
public String testBeforeService(String key,String value){ 
 
  return "key="+key+" value="+value; 
} 

前置通知攔截結(jié)果如下所示:

使用SpringBoot怎么對Spring AOP進行集成

后置返回通知

配置后置返回通知的代碼如下:

/** 
 * 后置返回通知 
 * 這里需要注意的是: 
 *   如果參數(shù)中的第一個參數(shù)為JoinPoint,則第二個參數(shù)為返回值的信息 
 *   如果參數(shù)中的第一個參數(shù)不為JoinPoint,則第一個參數(shù)為returning中對應的參數(shù) 
 * returning 限定了只有目標方法返回值與通知方法相應參數(shù)類型時才能執(zhí)行后置返回通知,否則不執(zhí)行,對于returning對應的通知方法參數(shù)為Object類型將匹配任何目標返回值 
 * @param joinPoint 
 * @param keys 
 */ 
@AfterReturning(value = "execution(* com.zkn.learnspringboot.web.controller..*.*(..))",returning = "keys") 
public void doAfterReturningAdvice1(JoinPoint joinPoint,Object keys){ 
 
  System.out.println("第一個后置返回通知的返回值:"+keys); 
} 
 
@AfterReturning(value = "execution(* com.zkn.learnspringboot.web.controller..*.*(..))",returning = "keys",argNames = "keys") 
public void doAfterReturningAdvice2(String keys){ 
 
  System.out.println("第二個后置返回通知的返回值:"+keys); 
} 

Controller里添加響應的請求處理信息來測試后置返回通知:

@RequestMapping("/testAfterReturning.do") 
public String testAfterReturning(String key){ 
 
  return "key=: "+key; 
} 
@RequestMapping("/testAfterReturning01.do") 
public Integer testAfterReturning01(Integer key){ 
 
  return key; 
} 

當發(fā)送請求為:http://localhost:8001/aop/testAfterReturning.do&#63;key=testsss&value=855sss時,處理結(jié)果如圖所示:

使用SpringBoot怎么對Spring AOP進行集成

當發(fā)送請求為:http://localhost:8001/aop/testAfterReturning01.do&#63;key=55553&value=855sss時,處理結(jié)果如圖所示:

使用SpringBoot怎么對Spring AOP進行集成

后置異常通知

后置異常通知的配置方式如下:

/** 
 * 后置異常通知 
 * 定義一個名字,該名字用于匹配通知實現(xiàn)方法的一個參數(shù)名,當目標方法拋出異常返回后,將把目標方法拋出的異常傳給通知方法; 
 * throwing 限定了只有目標方法拋出的異常與通知方法相應參數(shù)異常類型時才能執(zhí)行后置異常通知,否則不執(zhí)行, 
 *   對于throwing對應的通知方法參數(shù)為Throwable類型將匹配任何異常。 
 * @param joinPoint 
 * @param exception 
 */ 
@AfterThrowing(value = "executeService()",throwing = "exception") 
public void doAfterThrowingAdvice(JoinPoint joinPoint,Throwable exception){ 
  //目標方法名: 
  System.out.println(joinPoint.getSignature().getName()); 
  if(exception instanceof NullPointerException){ 
    System.out.println("發(fā)生了空指針異常!!!!!"); 
  } 
} 

Controller里配置響應的請求處理類:

@RequestMapping("/testAfterThrowing.do") 
public String testAfterThrowing(String key){ 
 
  throw new NullPointerException(); 
} 

后置異常通知方法的處理結(jié)果如下所示:

使用SpringBoot怎么對Spring AOP進行集成

后置最終通知

后置最終通知的配置方式如下:

/** 
 * 后置最終通知(目標方法只要執(zhí)行完了就會執(zhí)行后置通知方法) 
 * @param joinPoint 
 */ 
@After("executeService()") 
public void doAfterAdvice(JoinPoint joinPoint){ 
 
  System.out.println("后置通知執(zhí)行了!!!!"); 
} 

Controller類配置相應的請求處理類:

@RequestMapping("/testAfter.do") 
public String testAfter(String key){ 
 
  throw new NullPointerException(); 
} 
@RequestMapping("/testAfter02.do") 
public String testAfter02(String key){ 
 
  return key; 
} 

當發(fā)送請求為:http://localhost:8001/aop/testAfter.do&#63;key=55553&value=855sss

使用SpringBoot怎么對Spring AOP進行集成

當發(fā)送請求為:http://localhost:8001/aop/testAfter02.do&#63;key=55553&value=855sss

使用SpringBoot怎么對Spring AOP進行集成

環(huán)繞通知

環(huán)繞通知的配置方式如下:

/** 
 * 環(huán)繞通知: 
 *  環(huán)繞通知非常強大,可以決定目標方法是否執(zhí)行,什么時候執(zhí)行,執(zhí)行時是否需要替換方法參數(shù),執(zhí)行完畢是否需要替換返回值。 
 *  環(huán)繞通知第一個參數(shù)必須是org.aspectj.lang.ProceedingJoinPoint類型 
 */ 
@Around("execution(* com.zkn.learnspringboot.web.controller..*.testAround*(..))") 
public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint){ 
  System.out.println("環(huán)繞通知的目標方法名:"+proceedingJoinPoint.getSignature().getName()); 
  try { 
    Object obj = proceedingJoinPoint.proceed(); 
    return obj; 
  } catch (Throwable throwable) { 
    throwable.printStackTrace(); 
  } 
  return null; 
} 

Controller對應的請求處理類如下:

@RequestMapping("/testAroundService.do") 
public String testAroundService(String key){ 
 
  return "環(huán)繞通知:"+key; 
} 

當發(fā)送請求為:http://localhost:8001/aop/testAroundService.do&#63;key=55553

使用SpringBoot怎么對Spring AOP進行集成

當發(fā)送請求為:http://localhost:8001/aop/testAfter02.do&#63;key=55553&value=855sss時,不符合環(huán)繞通知的切入規(guī)則,所以環(huán)繞通知不會 執(zhí)行。

完整的AOP配置代碼如下:

package com.zkn.learnspringboot.aop; 
 
import com.alibaba.fastjson.JSON; 
import com.google.common.collect.Maps; 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.Signature; 
import org.aspectj.lang.annotation.*; 
import org.springframework.stereotype.Component; 
import org.springframework.web.context.request.RequestAttributes; 
import org.springframework.web.context.request.RequestContextHolder; 
 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpSession; 
import java.util.Enumeration; 
import java.util.Map; 
 
/** 
 * Created by zkn on 2016/11/18. 
 */ 
@Component 
@Aspect 
public class WebControllerAop { 
 
  //匹配com.zkn.learnspringboot.web.controller包及其子包下的所有類的所有方法 
  @Pointcut("execution(* com.zkn.learnspringboot.web.controller..*.*(..))") 
  public void executeService(){ 
 
  } 
 
  /** 
   * 前置通知,方法調(diào)用前被調(diào)用 
   * @param joinPoint 
   */ 
  @Before("executeService()") 
  public void doBeforeAdvice(JoinPoint joinPoint){ 
    System.out.println("我是前置通知!!!"); 
    //獲取目標方法的參數(shù)信息 
    Object[] obj = joinPoint.getArgs(); 
    //AOP代理類的信息 
    joinPoint.getThis(); 
    //代理的目標對象 
    joinPoint.getTarget(); 
    //用的最多 通知的簽名 
    Signature signature = joinPoint.getSignature(); 
    //代理的是哪一個方法 
    System.out.println(signature.getName()); 
    //AOP代理類的名字 
    System.out.println(signature.getDeclaringTypeName()); 
    //AOP代理類的類(class)信息 
    signature.getDeclaringType(); 
    //獲取RequestAttributes 
    RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); 
    //從獲取RequestAttributes中獲取HttpServletRequest的信息 
    HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST); 
    //如果要獲取Session信息的話,可以這樣寫: 
    //HttpSession session = (HttpSession) requestAttributes.resolveReference(RequestAttributes.REFERENCE_SESSION); 
    Enumeration<String> enumeration = request.getParameterNames(); 
    Map<String,String> parameterMap = Maps.newHashMap(); 
    while (enumeration.hasMoreElements()){ 
      String parameter = enumeration.nextElement(); 
      parameterMap.put(parameter,request.getParameter(parameter)); 
    } 
    String str = JSON.toJSONString(parameterMap); 
    if(obj.length > 0) { 
      System.out.println("請求的參數(shù)信息為:"+str); 
    } 
  } 
 
  /** 
   * 后置返回通知 
   * 這里需要注意的是: 
   *   如果參數(shù)中的第一個參數(shù)為JoinPoint,則第二個參數(shù)為返回值的信息 
   *   如果參數(shù)中的第一個參數(shù)不為JoinPoint,則第一個參數(shù)為returning中對應的參數(shù) 
   * returning 限定了只有目標方法返回值與通知方法相應參數(shù)類型時才能執(zhí)行后置返回通知,否則不執(zhí)行,對于returning對應的通知方法參數(shù)為Object類型將匹配任何目標返回值 
   * @param joinPoint 
   * @param keys 
   */ 
  @AfterReturning(value = "execution(* com.zkn.learnspringboot.web.controller..*.*(..))",returning = "keys") 
  public void doAfterReturningAdvice1(JoinPoint joinPoint,Object keys){ 
 
    System.out.println("第一個后置返回通知的返回值:"+keys); 
  } 
 
  @AfterReturning(value = "execution(* com.zkn.learnspringboot.web.controller..*.*(..))",returning = "keys",argNames = "keys") 
  public void doAfterReturningAdvice2(String keys){ 
 
    System.out.println("第二個后置返回通知的返回值:"+keys); 
  } 
 
  /** 
   * 后置異常通知 
   * 定義一個名字,該名字用于匹配通知實現(xiàn)方法的一個參數(shù)名,當目標方法拋出異常返回后,將把目標方法拋出的異常傳給通知方法; 
   * throwing 限定了只有目標方法拋出的異常與通知方法相應參數(shù)異常類型時才能執(zhí)行后置異常通知,否則不執(zhí)行, 
   *   對于throwing對應的通知方法參數(shù)為Throwable類型將匹配任何異常。 
   * @param joinPoint 
   * @param exception 
   */ 
  @AfterThrowing(value = "executeService()",throwing = "exception") 
  public void doAfterThrowingAdvice(JoinPoint joinPoint,Throwable exception){ 
    //目標方法名: 
    System.out.println(joinPoint.getSignature().getName()); 
    if(exception instanceof NullPointerException){ 
      System.out.println("發(fā)生了空指針異常!!!!!"); 
    } 
  } 
 
  /** 
   * 后置最終通知(目標方法只要執(zhí)行完了就會執(zhí)行后置通知方法) 
   * @param joinPoint 
   */ 
  @After("executeService()") 
  public void doAfterAdvice(JoinPoint joinPoint){ 
 
    System.out.println("后置通知執(zhí)行了!!!!"); 
  } 
 
  /** 
   * 環(huán)繞通知: 
   *  環(huán)繞通知非常強大,可以決定目標方法是否執(zhí)行,什么時候執(zhí)行,執(zhí)行時是否需要替換方法參數(shù),執(zhí)行完畢是否需要替換返回值。 
   *  環(huán)繞通知第一個參數(shù)必須是org.aspectj.lang.ProceedingJoinPoint類型 
   */ 
  @Around("execution(* com.zkn.learnspringboot.web.controller..*.testAround*(..))") 
  public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint){ 
    System.out.println("環(huán)繞通知的目標方法名:"+proceedingJoinPoint.getSignature().getName()); 
    try {//obj之前可以寫目標方法執(zhí)行前的邏輯 
      Object obj = proceedingJoinPoint.proceed();//調(diào)用執(zhí)行目標方法 
      return obj; 
    } catch (Throwable throwable) { 
      throwable.printStackTrace(); 
    } 
    return null; 
  } 
} 

完整的Controller類代碼如下:

package com.zkn.learnspringboot.web.controller; 
 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RestController; 
 
/** 
 * Created by zkn on 2016/11/19. 
 */ 
@RestController 
@RequestMapping("/aop") 
public class AopTestController { 
 
  @RequestMapping("/testBeforeService.do") 
  public String testBeforeService(String key,String value){ 
 
    return "key="+key+" value="+value; 
  } 
  @RequestMapping("/testAfterReturning.do") 
  public String testAfterReturning(String key){ 
 
    return "key=: "+key; 
  } 
  @RequestMapping("/testAfterReturning01.do") 
  public Integer testAfterReturning01(Integer key){ 
 
    return key; 
  } 
  @RequestMapping("/testAfterThrowing.do") 
  public String testAfterThrowing(String key){ 
 
    throw new NullPointerException(); 
  } 
  @RequestMapping("/testAfter.do") 
  public String testAfter(String key){ 
 
    throw new NullPointerException(); 
  } 
  @RequestMapping("/testAfter02.do") 
  public String testAfter02(String key){ 
 
    return key; 
  } 
  @RequestMapping("/testAroundService.do") 
  public String testAroundService(String key){ 
 
    return "環(huán)繞通知:"+key; 
  } 
} 

看完上述內(nèi)容,你們對使用SpringBoot怎么對Spring AOP進行集成有進一步的了解嗎?如果還想了解更多知識或者相關內(nèi)容,請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

文章標題:使用SpringBoot怎么對SpringAOP進行集成
標題網(wǎng)址:http://jinyejixie.com/article24/jjjece.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、全網(wǎng)營銷推廣、ChatGPT、微信公眾號App開發(fā)

廣告

聲明:本網(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)站網(wǎng)頁設計
潮安县| 安溪县| 衡阳县| 台北县| 土默特左旗| 建始县| 永城市| 家居| 合作市| 腾冲县| 丰城市| 定远县| 法库县| 平昌县| 陵川县| 汝城县| 吴旗县| 葫芦岛市| 石渠县| 龙胜| 塘沽区| 济宁市| 比如县| 苗栗市| 无极县| 伊吾县| 西乌| 金秀| 临西县| 班戈县| 崇信县| 忻城县| 靖江市| 襄汾县| 易门县| 乐昌市| 乌拉特后旗| 望奎县| 安西县| 通许县| 滦南县|