亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot2.x默認(rèn)使用的代理是cglib代理操作

 更新時(shí)間:2021年08月09日 09:51:27   作者:淘氣小子  
這篇文章主要介紹了springboot2.x默認(rèn)使用的代理是cglib代理操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

背景

因?yàn)轫?xiàng)目?jī)?yōu)化,打算寫個(gè)日志的切面類,于是起了個(gè)springboot 工程,在這里面測(cè)試。結(jié)果在springboot 里面測(cè)試正常,能正確打印日志,但是把代碼復(fù)制到實(shí)際項(xiàng)目中,在進(jìn)入切面打印日志的時(shí)候總是報(bào)錯(cuò),報(bào)空指針錯(cuò)誤。

經(jīng)調(diào)試發(fā)現(xiàn)每次都是在獲取注解上的屬性時(shí)報(bào)錯(cuò)。當(dāng)時(shí)百思不得解。后來靈光一閃,想到可能是項(xiàng)目中獲取到的是接口方法,而springboot是實(shí)現(xiàn)類的method ,所以可以拿到注解的屬性。

但是仔細(xì)一想,Springboot里面也是接口,難道不應(yīng)該走JDK動(dòng)態(tài)代理嗎?那拿到這個(gè)方法的應(yīng)該也是接口的方法,帶著這個(gè)疑問,我開始了我的探索之旅。

驗(yàn)證

springboot 項(xiàng)目

cglib動(dòng)態(tài)代理

spring 項(xiàng)目

JDK動(dòng)態(tài)代理

發(fā)現(xiàn)springBoot 竟然走的是cglib代理,起代理的是實(shí)現(xiàn)類,所以能拿到方法上注解的屬性,而我的項(xiàng)目是個(gè)傳統(tǒng)的spring 項(xiàng)目,service是接口,走的是JDK動(dòng)態(tài)代理,通過切點(diǎn)拿到的是接口的方法,而接口上又沒有注解,所以按照springboot的寫法是拿不到注解的,拿不到注解也就拿不到注解屬性,所以報(bào)錯(cuò)。

解決辦法

springboot的寫法

 private Method getMethod(ProceedingJoinPoint joinPoint) throws NoSuchMethodException {
        //獲取方法簽名
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
        return method;
    }
    
 private String getAnnotationDesc(ProceedingJoinPoint joinPoint) throws NoSuchMethodException {
        Method method = getMethod(joinPoint);
        String value = method.getAnnotation(MyLog.class).value();
        return value;
    }

spring 的寫法

private Method getMethod(ProceedingJoinPoint joinPoint) throws NoSuchMethodException {
        //獲取方法簽名
        Class<?> targetClass = joinPoint.getTarget().getClass();
        String methodName = joinPoint.getSignature().getName();
        Class[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getParameterTypes();
        Method method = targetClass.getMethod(methodName, parameterTypes);
        return method;
    }
private String getAnnotationDesc(ProceedingJoinPoint joinPoint) throws NoSuchMethodException {
        Method method = getMethod(joinPoint);
        String value = method.getAnnotation(MyLog.class).value();
        return value;
    }

可以看到spring項(xiàng)目的方法是先獲取目標(biāo)類,然后再通過目標(biāo)類獲取目標(biāo)方法,然后再獲取方法上的注解。

深度追蹤

springboot 為什么將默認(rèn)的代理改成了cglib,這會(huì)導(dǎo)致什么問題?如果我們想要事務(wù)走JDK動(dòng)態(tài)代理,該如何做?

帶著這些疑問,我翻閱了springboot的相關(guān)issue ,發(fā)現(xiàn)很多人提這個(gè)問題。

先關(guān)issue如下:

issue1

issue2

issue2

springboot團(tuán)隊(duì)之所以默認(rèn)的代理模式設(shè)置成cglib代理,看看spring的官方團(tuán)隊(duì)是怎么解釋的

This was changed in 1.4 (see 5423). We've generally found cglib proxies less likely to cause unexpected cast exceptions.

他們認(rèn)為使用cglib更不容易出現(xiàn)轉(zhuǎn)換錯(cuò)誤。springboot 默認(rèn)的配置文件的位置在

/org/springframework/boot/spring-boot-autoconfigure/2.1.7.RELEASE/spring-boot-autoconfigure-2.1.7.RELEASE.jar!/META-INF/spring-configuration-metadata.json

{
      "name": "spring.aop.proxy-target-class",
      "type": "java.lang.Boolean",
      "description": "Whether subclass-based (CGLIB) proxies are to be created (true), as opposed to standard Java interface-based proxies (false).",
      "defaultValue": true
    },

如果在事務(wù)中強(qiáng)制使用JDK動(dòng)態(tài)代理,以往的知識(shí)告訴我們,我們需要將proxyTargetClass 設(shè)置成false,于是我們?cè)趕pringboot 中發(fā)現(xiàn)注解@EnableTransactionManagement 或者@EnableAspectJAutoProxy默認(rèn)就為false,說明這里面的屬性不起作用

@EnableAspectJAutoProxy(proxyTargetClass = false)
@EnableTransactionManagement(proxyTargetClass = false)

同理 @EnableCaching 上的proxyTargetClass 屬性也是失效的。如果偏要springboot 走JDK動(dòng)態(tài)代理,那么需要在application.properties里面配置

spring.aop.proxy-target-class=false

此時(shí)項(xiàng)目中走的就是JDK動(dòng)態(tài)代理。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論