最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

關(guān)于Spring AOP使用時(shí)的一些問(wèn)題匯總

 更新時(shí)間:2020年10月25日 15:15:04   作者:zero  
這篇文章主要給大家匯總介紹了關(guān)于Spring AOP使用時(shí)的一些問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

在使用AOP的時(shí)候遇到了一些問(wèn)題,特此記錄一下

首先寫(xiě)一個(gè)常用的AOP切片

切片類(lèi)AopLog

package com.mantis.aop.aspect;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mantis.aop.common.util.DataUtil;
import eu.bitwalker.useragentutils.UserAgent;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.validation.BeanPropertyBindingResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:執(zhí)行順序 正常順序 Around Before Method.invoke Around After AfterReturning
 * 異常順序 Around Before Method.invoke After AfterThrowing
 * @author: wei.wang
 * @since: 2020/4/4 13:47
 * @history: 1.2020/4/4 created by wei.wang
 */
@Aspect
@Component
public class AopLog {

 private static Logger logger = LoggerFactory.getLogger(AopLog.class);

 /**
  * 定義切點(diǎn),切點(diǎn)為com.smec.fin.controller包和子包里任意方法的執(zhí)行和service層所有方法的執(zhí)行
  */
 @Pointcut("execution(public * com.mantis.aop.controller..*.*(..))")
 public void log() {
  // 定義切點(diǎn)
 }

 /**
  * 定義切點(diǎn),切點(diǎn)為com.smec.fin.controller包和子包里任意方法的執(zhí)行和service層所有方法的執(zhí)行
  */
 @Pointcut("execution(public * com.mantis.aop.service.impl..*.*(..))")
 public void log2() {
  // 定義切點(diǎn)
 }

 /**
  * 環(huán)繞通知
  *
  * @param point
  * @return
  * @throws Throwable
  */
 @Around("log2()")
 public Object aroundLog(ProceedingJoinPoint point) throws Throwable {
  logger.info("開(kāi)始執(zhí)行環(huán)繞操作");
  Object result = point.proceed();
  logger.info("執(zhí)行環(huán)繞操作結(jié)束,返回值:{}", result);
  return result;
 }
}

服務(wù)類(lèi)AopServiceImpl

package com.mantis.aop.service.impl;

import com.mantis.aop.service.AopService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @author: wei.wang
 * @since: 2020/10/24 12:45
 * @history: 1.2020/10/24 created by wei.wang
 */
@Service
public class AopServiceImpl implements AopService {


 @Override
 public void testAop(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testAop" + str);
  this.testAop2("testFinalMethod");
 }

 @Override
 public void testAop2(String str) {
  //this.testFinalMethod("testFinalMethod");
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl." + str);
 }

 public final void testFinalMethod(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod" + str);
 }

 public void testFinalMethod2(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod" + str);
 }
}

1.同方法運(yùn)行問(wèn)題

在使用AOP時(shí)我們發(fā)現(xiàn)存在同類(lèi)中調(diào)用時(shí)切點(diǎn)失效問(wèn)題,在執(zhí)行時(shí)我們發(fā)現(xiàn)只執(zhí)行了testAop的切點(diǎn),testAop2的切點(diǎn)沒(méi)有執(zhí)行,這是因?yàn)榻?jīng)過(guò)AOP代理后的對(duì)象都已經(jīng)不是原來(lái)的對(duì)象了,而是加入了增強(qiáng)方法的代理對(duì)象,使用代理對(duì)象調(diào)用時(shí)可以執(zhí)行增強(qiáng)方法,但是這里是使用this調(diào)用的,也就是AopServiceImpl,因?yàn)椴皇谴韺?duì)象就沒(méi)有增強(qiáng)方法。

2020-10-24 13:31:06.261 INFO 13344 --- [nio-9000-exec-1] com.mantis.aop.controller.AopController : 方法開(kāi)始執(zhí)行
2020-10-24 13:31:06.264 INFO 13344 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 開(kāi)始執(zhí)行環(huán)繞操作
com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2
com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod

2020-10-24 13:31:06.274 INFO 13344 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null

用@Autowired或Resource引入自身依賴(lài)

使用注解方法引入自身代理依賴(lài),注意使用構(gòu)造的方式會(huì)有循環(huán)依賴(lài)問(wèn)題

 @Autowired
 AopServiceImpl aopService;

 @Override
 public void testAop(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testAop" + str);
  aopService.testAop2("testFinalMethod");
  System.out.println();
 }
2020-10-24 13:36:33.477 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.controller.AopController : 方法開(kāi)始執(zhí)行
2020-10-24 13:36:33.480 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 開(kāi)始執(zhí)行環(huán)繞操作
com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2
2020-10-24 13:36:33.488 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 開(kāi)始執(zhí)行環(huán)繞操作
com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod
2020-10-24 13:36:33.488 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null

2020-10-24 13:36:33.490 INFO 12528 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null

開(kāi)啟暴露代理類(lèi),AopContext.currentProxy()方式獲取代理類(lèi)

通過(guò)暴露代理類(lèi)方式,實(shí)際原理是把代理類(lèi)添加到當(dāng)前請(qǐng)求的ThreadLocal里面,然后在使用時(shí)從ThreadLocal中獲取代理類(lèi),再調(diào)用對(duì)應(yīng)的方法

在主方法上加入@EnableAspectJAutoProxy(exposeProxy=true),然后從AOP上下文中獲取代理

 @Override
 public void testAop(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testAop" + str);
  AopServiceImpl service = AopContext.currentProxy() != null ? (AopServiceImpl) AopContext.currentProxy() : this;
  service.testAop2("testFinalMethod");
  System.out.println();
 }
2020-10-24 13:38:31.031 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.controller.AopController : 方法開(kāi)始執(zhí)行
2020-10-24 13:38:31.035 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 開(kāi)始執(zhí)行環(huán)繞操作
com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2
2020-10-24 13:38:31.047 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 開(kāi)始執(zhí)行環(huán)繞操作
com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod
2020-10-24 13:38:31.048 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null

2020-10-24 13:38:31.050 INFO 18828 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null

2.final關(guān)鍵字問(wèn)題

Spring AOP默認(rèn)使用cglib,會(huì)生成目標(biāo)對(duì)象的子類(lèi)代理對(duì)象。調(diào)用目標(biāo)對(duì)象的方法,實(shí)際上是調(diào)用代理對(duì)象的方法。由于子類(lèi)能夠繼承父類(lèi)的方法,因此一般情況下目標(biāo)類(lèi)的方法,代理對(duì)象都會(huì)有。但是當(dāng)目標(biāo)類(lèi)中某個(gè)方法帶有final關(guān)鍵字時(shí),這個(gè)方法不能被重寫(xiě),因此代理對(duì)象中沒(méi)有這個(gè)方法,因此會(huì)調(diào)用目標(biāo)對(duì)象的方法。

帶final關(guān)鍵字

因?yàn)閠estFinalMethod方法中存在final關(guān)鍵字,導(dǎo)致無(wú)法重寫(xiě),結(jié)果代理對(duì)象就無(wú)法生成這個(gè)方法,因此調(diào)用目標(biāo)對(duì)象方法,導(dǎo)致沒(méi)有被增強(qiáng)

 @Override
 public void testAop(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testAop" + str);
  AopServiceImpl service = AopContext.currentProxy() != null ? (AopServiceImpl) AopContext.currentProxy() : this;
  service.testFinalMethod("testFinalMethod");
  System.out.println();
 }
 
 public final void testFinalMethod(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod" + str);
 }
2020-10-24 13:47:46.907 INFO 15204 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 開(kāi)始執(zhí)行環(huán)繞操作
com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2
com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethodtestFinalMethod

2020-10-24 13:47:46.916 INFO 15204 --- [nio-9000-exec-1] com.mantis.aop.aspect.AopLog    : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null

不帶final關(guān)鍵字

因?yàn)閠estFinalMethod方法中不存在final關(guān)鍵字,所以正常執(zhí)行增強(qiáng)。

 @Override
 public void testAop(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testAop" + str);
  AopServiceImpl service = AopContext.currentProxy() != null ? (AopServiceImpl) AopContext.currentProxy() : this;
  service.testFinalMethod2("testFinalMethod");
  System.out.println();
 }
 
 public final void testFinalMethod2(String str) {
  System.out.println("com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethod" + str);
 }
2020-10-24 13:50:51.018 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.controller.AopController : 方法開(kāi)始執(zhí)行
2020-10-24 13:50:51.021 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog    : 開(kāi)始執(zhí)行環(huán)繞操作
com.mantis.aop.service.AopService.AopServiceImpl.testAoptest2
2020-10-24 13:50:51.029 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog    : 開(kāi)始執(zhí)行環(huán)繞操作
com.mantis.aop.service.AopService.AopServiceImpl.testFinalMethodtestFinalMethod
2020-10-24 13:50:51.030 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog    : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null

2020-10-24 13:50:51.031 INFO 13532 --- [nio-9000-exec-2] com.mantis.aop.aspect.AopLog    : 執(zhí)行環(huán)繞操作結(jié)束,返回值:null

總結(jié)

到此這篇關(guān)于Spring AOP使用時(shí)的一些問(wèn)題匯總的文章就介紹到這了,更多相關(guān)Spring AOP使用時(shí)的問(wèn)題內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 一文搞懂Java?ScheduledExecutorService的使用

    一文搞懂Java?ScheduledExecutorService的使用

    JUC包(java.util.concurrent)中提供了對(duì)定時(shí)任務(wù)的支持,即ScheduledExecutorService接口。本文主要對(duì)ScheduledExecutorService的使用進(jìn)行簡(jiǎn)單的介紹,需要的可以參考一下
    2022-11-11
  • Spring boot + LayIM + t-io 實(shí)現(xiàn)文件上傳、 監(jiān)聽(tīng)用戶(hù)狀態(tài)的實(shí)例代碼

    Spring boot + LayIM + t-io 實(shí)現(xiàn)文件上傳、 監(jiān)聽(tīng)用戶(hù)狀態(tài)的實(shí)例代碼

    這篇文章主要介紹了Spring boot + LayIM + t-io 實(shí)現(xiàn)文件上傳、 監(jiān)聽(tīng)用戶(hù)狀態(tài)的實(shí)例代碼,需要的朋友可以參考下
    2017-12-12
  • Java的String類(lèi)中的startsWith方法和endsWith方法示例詳解

    Java的String類(lèi)中的startsWith方法和endsWith方法示例詳解

    大家應(yīng)該都知道startsWith()方法用于檢測(cè)字符串是否以指定的前綴開(kāi)始,endsWith()方法用于測(cè)試字符串是否以指定的后綴結(jié)束,本文就Java的String類(lèi)中的startsWith方法和endsWith方法給大家詳細(xì)講解,感興趣的朋友一起看看吧
    2023-11-11
  • java的Object里wait()實(shí)現(xiàn)原理講解

    java的Object里wait()實(shí)現(xiàn)原理講解

    這篇文章主要介紹了java的Object里wait()實(shí)現(xiàn)原理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringBoot?MDC全局鏈路最新完美解決方案

    SpringBoot?MDC全局鏈路最新完美解決方案

    MDC 在 Spring Boot 中的作用是為日志事件提供上下文信息,并將其與特定的請(qǐng)求、線(xiàn)程或操作關(guān)聯(lián)起來(lái),通過(guò)使用 MDC,可以更好地理解和分析日志,并在多線(xiàn)程環(huán)境中確保日志的準(zhǔn)確性和一致性,這篇文章主要介紹了SpringBoot?MDC全局鏈路解決方案,需要的朋友可以參考下
    2023-08-08
  • Java讀取xml文件的五種方式

    Java讀取xml文件的五種方式

    在編寫(xiě)與 XML 數(shù)據(jù)交互的現(xiàn)代軟件應(yīng)用時(shí),有效地讀取和解析 XML 文件是至關(guān)重要的,本文旨在探討 Java 中處理 XML 文件的五種主要方法:DOM、SAX、StAX、JAXB 和 JDOM,我們將詳細(xì)介紹每種方法的工作原理、典型用途以及如何在 Java 程序中實(shí)現(xiàn)它們
    2024-05-05
  • Springboot 如何實(shí)現(xiàn)filter攔截token驗(yàn)證和跨域

    Springboot 如何實(shí)現(xiàn)filter攔截token驗(yàn)證和跨域

    這篇文章主要介紹了Springboot 如何實(shí)現(xiàn)filter攔截token驗(yàn)證和跨域操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java中的CAS鎖機(jī)制(無(wú)鎖、自旋鎖、樂(lè)觀鎖、輕量級(jí)鎖)詳解

    Java中的CAS鎖機(jī)制(無(wú)鎖、自旋鎖、樂(lè)觀鎖、輕量級(jí)鎖)詳解

    這篇文章主要介紹了Java中的CAS鎖機(jī)制(無(wú)鎖、自旋鎖、樂(lè)觀鎖、輕量級(jí)鎖)詳解,CAS算法的作用是解決多線(xiàn)程條件下使用鎖造成性能損耗問(wèn)題的算法,保證了原子性,這個(gè)原子操作是由CPU來(lái)完成的,需要的朋友可以參考下
    2024-01-01
  • SpringMVC中Controller層獲取前端請(qǐng)求參數(shù)的方式匯總

    SpringMVC中Controller層獲取前端請(qǐng)求參數(shù)的方式匯總

    這篇文章主要介紹了SpringMVC中Controller層獲取前端請(qǐng)求參數(shù)的幾種方式,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • Java?SimpleDateFormat線(xiàn)程不安全問(wèn)題

    Java?SimpleDateFormat線(xiàn)程不安全問(wèn)題

    這篇文章詳細(xì)介紹了如可解決impleDateFormat線(xiàn)程不安全的問(wèn)題,對(duì)多線(xiàn)程問(wèn)題感興趣的同學(xué)可以參考閱讀本文
    2023-03-03

最新評(píng)論

南和县| 安岳县| 霸州市| 丰都县| 全州县| 盐池县| 台安县| 英山县| 巍山| 黄浦区| 中江县| 云浮市| 时尚| 迁西县| 兴山县| 西青区| 巫溪县| 宣汉县| 大连市| 洪湖市| 茌平县| 永新县| 长宁县| 贡嘎县| 大宁县| 萨嘎县| 武乡县| 牡丹江市| 图片| 鹤岗市| 进贤县| 旺苍县| 南郑县| 临泽县| 大田县| 岑溪市| 紫金县| 乌拉特后旗| 泾源县| 桂阳县| 太仆寺旗|