java.lang.ClassCastException 同包同类名强转异常

目录

问题

今天碰到了一个很诡异的问题,有一段代码在执行强转时居然提示自己不能转自己,还真的是头一回碰到这种问题,代码及异常如下

ComplexOrderDbShardingAlgorithm complexOrderDbShardingAlgorithm = (ComplexOrderDbShardingAlgorithm) ReflectUtil.getFieldValue(dataBaseShardingStrategy, "shardingAlgorithm");
complexOrderDbShardingAlgorithm.init(tbShardingConfigServiceImpl);
Caused by: java.lang.ClassCastException: com.chinawu.cloud.ss.config.sharding.ComplexOrderDbShardingAlgorithm cannot be cast to com.chinawu.cloud.ss.config.sharding.ComplexOrderDbShardingAlgorithm
	at com.chinawu.cloud.ss.config.sharding.StartupConfig.loadLogInit(StartupConfig.java:57) ~[classes/:na]
	at com.chinawu.cloud.ss.config.sharding.StartupConfig.init(StartupConfig.java:39) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
	... 23 common frames omitted

解决

问题太不可思议了,业务代码原先本地调试都正常的,怎么就出现该问题了呢?
排查后发现项目pom中原本注释的springboot 热部署依赖这次因为有个业务需要调试就直接打开了,结果就导致了本次强转的问题。

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

注释后重新测试了下,果然世界舒畅了。。。

上一篇:设计模式之工厂三姐妹① 简单工厂与工厂模式系列博客


下一篇:设计模式之享元模式