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

詳解spring中aop不生效的幾種解決辦法

 更新時間:2020年06月24日 14:17:35   作者:菩提樹下的楊過  
這篇文章主要介紹了詳解spring中aop不生效的幾種解決辦法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

先看下這個問題的背景:假設(shè)有一個spring應(yīng)用,開發(fā)人員希望自定義一個注解@Log,可以加到指定的方法上,實現(xiàn)自動記錄日志(入?yún)?、出參、響?yīng)耗時這些)

package com.cnblogs.yjmyzz.springbootdemo.aspect;
 
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Log {
 
}

然后再寫一個Aspect來解析這個注解,對打了Log注解的方法進行增強處理 

package com.cnblogs.yjmyzz.springbootdemo.aspect;
 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
 
import java.lang.reflect.Method;
 
@Component
@Aspect
public class LogAspect {
 
  @Pointcut("execution (* com.cnblogs.yjmyzz.springbootdemo.service..*.*(..))")
  public void logPointcut() {
 
  }
 
  @Around("logPointcut()")
  public void around(JoinPoint point) {
    String methodName = point.getSignature().getName();
    Object[] args = point.getArgs();
    Class<?>[] argTypes = new Class[point.getArgs().length];
    for (int i = 0; i < args.length; i++) {
      argTypes[i] = args[i].getClass();
    }
    Method method = null;
    try {
      method = point.getTarget().getClass().getMethod(methodName, argTypes);
    } catch (Exception e) {
      e.printStackTrace();
    }
    //獲取方法上的注解
    Log log = method.getAnnotation(Log.class);
    if (log != null) {
      //演示方法執(zhí)行前,記錄一行日志
      System.out.println("before:" + methodName);
    }
    try {
      //執(zhí)行方法
      ((ProceedingJoinPoint) point).proceed();
    } catch (Throwable throwable) {
      throwable.printStackTrace();
    } finally {
      if (log != null) {
        //演示方法執(zhí)行后,記錄一行日志
        System.out.println("after:" + methodName);
      }
    }
  }
}

寫一個測試Service類:

package com.cnblogs.yjmyzz.springbootdemo.service;
 
import com.cnblogs.yjmyzz.springbootdemo.aspect.Log;
import org.springframework.stereotype.Component;
 
@Component
public class HelloService {
   
  @Log
  public void sayHi(String msg) {
    System.out.println("\tsayHi:" + msg);
  }
 
  public void anotherSayHi(String msg) {
    this.sayHi(msg);
  }
 
}

最后來跑一把:

package com.cnblogs.yjmyzz.springbootdemo;
 
import com.cnblogs.yjmyzz.springbootdemo.service.HelloService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
 
/**
 * @author 菩提樹下的楊過
 */
@ComponentScan("com.cnblogs.yjmyzz")
@Configuration
@EnableAspectJAutoProxy
public class SampleApplication {
 
  public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SampleApplication.class);
    HelloService helloService = context.getBean(HelloService.class);
    helloService.sayHi("hi-1");
    System.out.println("\n");
    helloService.anotherSayHi("hi-2");
  }
}

輸出如下:

顯然HelloService中的anotherSayHi方法,并未被aop增強。 原因其實很簡單,了解AOP原理的同學(xué)想必都知道,AOP的實現(xiàn)有二類,如果是基于接口的,會采用動態(tài)代理,生成一個代理類,如果是基于類的,會采用CGLib生成子類,然后在子類中擴展父類中的方法。

本文中HelloService并不是一個接口,所以從上圖的斷點中可以看出,當(dāng)Spring運行時,HelloService被增加為...EnhancerBySpringCGLib...。但是當(dāng)調(diào)用到anotherSayHi時

方法的調(diào)用方,其實是原始的HelloSerfvice實例,即:是未經(jīng)過Spring AOP增強的對象實例。所以解決問題的思路就有了,想辦法用增強后的HelloService實例來調(diào)用!

方法一:用Autowired 注入自身的實例

這個方法,第一眼看上去感覺有些怪,自己注入自己,感覺有點象遞歸/死循環(huán)的搞法,但確實可以work,Spring在解決循環(huán)依賴上有自己的處理方式,避免了死循環(huán)。

方法二:從Spring上下文獲取增強后的實例引用

原理與方法一其實類似,不多解釋。

方法三: 利用AopContext

不過這個方法要注意的是,主類入口上,必須加上exporseProxy=true,參考下圖:

最后來驗證下這3種方法是否生效:

從運行結(jié)果上看,3種方法都可以解決這個問題?!?/p>

到此這篇關(guān)于詳解spring中aop不生效的幾種解決辦法的文章就介紹到這了,更多相關(guān)spring中aop不生效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

作者:菩提樹下的楊過
出處:http://yjmyzz.cnblogs.com

相關(guān)文章

  • Mybatis框架之代理模式(Proxy Pattern)的實現(xiàn)

    Mybatis框架之代理模式(Proxy Pattern)的實現(xiàn)

    本文主要介紹了MyBatis框架中使用代理模式ProxyPattern的原理和實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-11-11
  • SpringBoot實現(xiàn)圖片上傳及本地訪問

    SpringBoot實現(xiàn)圖片上傳及本地訪問

    在SpringBoot項目中,處理靜態(tài)文件訪問尤其是實時更新的文件如商品圖片,可通過配置WebMvcConfig將本地文件映射到URL路徑上,以解決重啟項目才能訪問文件的問題,本文詳解如何保存和訪問這些文件,幫助開發(fā)者優(yōu)化項目文件管理
    2022-09-09
  • Java正則驗證字串符RegexValidator類使用

    Java正則驗證字串符RegexValidator類使用

    正則驗證字串符是一種強大的工具,可以幫助程序員在處理字符串時輕松進行復(fù)雜匹配,本文將介紹正則表達式的概念、語法和在編程中的應(yīng)用,并通過實例演示如何使用正則表達式進行字符串匹配、替換和提取等操作
    2023-11-11
  • 深入解析Spring?TransactionTemplate?高級用法(示例代碼)

    深入解析Spring?TransactionTemplate?高級用法(示例代碼)

    TransactionTemplate是?Spring?框架中一個強大的工具,它允許開發(fā)者以編程方式控制事務(wù),通過本文的詳細解析和示例代碼,我們可以看到如何使用?`TransactionTemplate`?來執(zhí)行事務(wù)性操作,并處理高級事務(wù)場景,感興趣的朋友跟隨小編一起看看吧
    2025-02-02
  • Spring Boot整合MyBatis-Plus實現(xiàn)CRUD操作的示例代碼

    Spring Boot整合MyBatis-Plus實現(xiàn)CRUD操作的示例代碼

    本文主要介紹了Spring Boot整合MyBatis-Plus實現(xiàn)CRUD操作,可以快速實現(xiàn)數(shù)據(jù)庫的增刪改查操作,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-04-04
  • SpringMVC異常處理的三種方式

    SpringMVC異常處理的三種方式

    在SpringMVC中異常處理是一個重要的方面,它幫助我們有效地處理應(yīng)用程序中的異常情況,提高用戶體驗和系統(tǒng)的穩(wěn)定性,這篇文章主要給大家介紹了關(guān)于SpringMVC異常處理的三種方式,需要的朋友可以參考下
    2024-02-02
  • SpringBoot項目如何訪問jsp頁面的示例代碼

    SpringBoot項目如何訪問jsp頁面的示例代碼

    本篇文章主要介紹了SpringBoot項目如何訪問jsp頁面的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處

    java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處

    java 中的static關(guān)鍵字和final關(guān)鍵字的不同之處,需要的朋友可以參考一下
    2013-03-03
  • springboot vue組件開發(fā)實現(xiàn)接口斷言功能

    springboot vue組件開發(fā)實現(xiàn)接口斷言功能

    這篇文章主要為大家介紹了springboot+vue組件開發(fā)實現(xiàn)接口斷言功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • Java實現(xiàn)滑動窗口算法的示例代碼

    Java實現(xiàn)滑動窗口算法的示例代碼

    滑動窗口算法是一種高效解決子數(shù)組、子字符串問題的算法,廣泛應(yīng)用于數(shù)據(jù)流處理、網(wǎng)絡(luò)限流和字符串操作等場景,本文將詳細解析滑動窗口算法的核心思想、常見問題及其實現(xiàn)方式,需要的朋友可以參考下
    2025-03-03

最新評論

班玛县| 习水县| 东城区| 随州市| 察隅县| 青龙| 互助| 安新县| 屯留县| 静海县| 明星| 南陵县| 安图县| 呼伦贝尔市| 三门县| 绥中县| 南和县| 江阴市| 喀喇| 石棉县| 梁山县| 云阳县| 建阳市| 秦皇岛市| 衡水市| 石家庄市| 靖州| 扶沟县| 安新县| 汉寿县| 卓资县| 博乐市| 广饶县| 丰原市| 海门市| 夹江县| 石楼县| 平舆县| 隆化县| 长子县| 淳安县|