SpringBoot整合Dubbo报错: java.lang.ClassCastException

本文主要探讨了在SpringBoot整合Dubbo时遇到的`java.lang.ClassCastException`问题。该错误源于类加载器的不同,导致JVM中类的唯一标识变化。为解决这个问题,可以参照Spring Boot DevTools的文档,在resources目录下创建`META-INF/spring-devtools.properties`文件,并配置相关属性,以避免类加载顺序导致的异常。

Caused by: java.lang.ClassCastException这种错误,由于是加了以下的配置出现的(原因很明显了)

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>

原因:

By default, any open project in your IDE will be loaded using the 
“restart” classloader, and any regular .jar file will be loaded using 
the “base” classloader. If you work on a multi-module project, and not 
each module is imported into your IDE, you may need to customize 
things. To do this you can create a 
META-INF/spring-devtools.properties file.

The spring-devtools.properties file can contain restart.exclude. and 
restart.include. prefixed properties. The include elements are items 
that should be pulled up into the “restart” classloader, and the 
exclude elements are items that should be pushed down into the “base” 
classloader. The value of the property is a regex pattern that will be 
applied to the classpath.
翻译:
默认情况下,将使用
“restart”类加载器,任何常规的.jar文件都将使用
“base”类加载器。如果你在一个多模块项目上工作,而不是
每个模块都导入到您的IDE中,您可能需要自定义
东西。为此,您可以创建一个
meta-inf/spring-devtools.properties文件。
spring-devtools.properties文件可以包含restart.exclude。和
重新启动.include。带前缀的属性。include元素是项
它应该被拉到“重新启动”类加载器中,并且
排除元素是应该下推到“基”中的项类加载器。属性的值是将应用于类路径。

在Spring Boot DevTools的文档文档中找到上面的描述。
原理:
类加载器不同!
因为java的类在JVM中是类加载器加上类名做唯一标识.
所以不同类加载顺序,会导致异常!

这里附带一个解决方案
解决方案就是在resources目录下面创建META_INF文件夹,然后创建spring-devtools.properties文件,文件加上类似下面的配置:

restart.exclude.companycommonlibs=/mycorp-common-[\\w-]+\.jar
restart.include.projectcommon=/mycorp-myproj-[\\w-]+\.jar

https://blog.csdn.net/xichenguan/article/details/73741835?utm_source=blogxgwz0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值