SpringBoot(三)


SpringBoot啟動報包沖突have different Class objects for the type

現象:

and the class loader (instance of sun/misc/Launcher$AppClassLoader) for interface  DeviceFeignInterFace. have different Class objects for the type MobileResponseClass used in the signature

Caused by: java.lang.LinkageError: loader constraint violation in interface itable initialization: when resolving method "com.sun.proxy.$Proxy175.insert(Lcom/sunego/commerce/wvp/feign/vo/SnapVO;)Lcom/sunego/commerce/base/api/MobileResponse;" the class loader (instance of org/springframework/boot/devtools/restart/classloader/RestartClassLoader) of the current class, com/sun/proxy/$Proxy175, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for interface com/sunego/commerce/wvp/feign/interfaces/DeviceFeign have different Class objects for the type com/sunego/commerce/base/api/MobileResponse used in the signature  at java.lang.Class.getDeclaredConstructors0(Native Method)  at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)  at java.lang.Class.getConstructor0(Class.java:3075)  at java.lang.Class.getConstructor(Class.java:1825)  at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:729)  at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:65)  at feign.Feign$Builder.target(Feign.java:269)  at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:38)  at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:301)  at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:330)  at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:310)  at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:169)  ... 49 common frames omitted

問題分析:

1、同一個包,不同版本,導致包沖突。(非此原因)

2、不同的包,但包內的類路徑相同,造成同名類沖突。(非此原因)

3、SpringBoot啟用devtools后,可以實現熱部署,熱部署的深層原理是使用了兩個ClassLoader,一個Classloader加載那些不會改變的類(第三方Jar包),另一個ClassLoader加載會更改的類(項目內的模塊),稱為restart ClassLoader。

例如,A項目下,有a1,a2,a3三個模塊,B項目下有b1,b2兩個模塊,如果A.a1中引入了B.b1,那么A.a1和B.b1會被分別加載到不同的ClassLoader中。

但如果A.a1依賴了A.a2(項目內依賴),同時B.b1也依賴了A.a2(項目外依賴),那就會導致A.a2在兩個ClassLoader中都被加載,相當於被加載兩次,所以就造成包沖突了。

 

解決方案:

方案一、不使用spring-boot-devtools

方案二、解決方案就是在resources目錄下面創建META-INF文件夾,然后創建spring-devtools.properties文件,文件加上類似下面的配置:

把沖突的包排除 restart.exclude.companycommonlibs=xxx.jar

方案三、把公用包從項目模塊中移除出去,都使用三方依賴的方式。

 

參考:

spring-boot-devtools實現熱部署

https://www.cnblogs.com/liu2-/p/9118393.html

 

SpringBoot使用devtools導致的類型轉換異常

https://blog.csdn.net/m0_38043362/article/details/78064539


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM