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

posted @ 2021-05-28 11:14  迷走神经  阅读(1383)  评论(0编辑  收藏  举报