与 spring-boot-devtools 相关的类加载器问题

zhu*_*wei 6 classloader dozer spring-boot

背景:Spring Boot项目,添加商品和商品价目表

Goods:
List<GoodsPrice> pricelist;
Run Code Online (Sandbox Code Playgroud)

在控制器中首先将货物形式转换为货物(通过推土机),然后保存货物,保存货物后迭代货物价目表以填充货物 ID。

goods.getPriceList().forEach(p -> p.setGoodsId(goods.getId()));
Run Code Online (Sandbox Code Playgroud)

迭代商品价目表时,抛出异常:

java.lang.ClassCastException: com.foo.goods.model.GoodsPrice cannot be cast to com.foo.goods.model.GoodsPrice
at com.foo.goods.service.GoodsService$$Lambda$11/310447431.accept(Unknown Source) ~[na:na]
at java.util.ArrayList.forEach(ArrayList.java:1249) ~[na:1.8.0_51]
at com.foo.goods.service.GoodsService.saveGoods(GoodsService.java:34) ~[classes/:na]
Run Code Online (Sandbox Code Playgroud)

有人提醒我这个与classloader有关的异常,在eclipse调试模式下,我输出了GoodsPrice的ClassLoader:

sun.misc.Launcher$AppClassLoader@14dad5dc
Run Code Online (Sandbox Code Playgroud)

和货物: org.springframework.boot.devtools.restart.classloader.RestartClassLoader@591c6338

确实存在差异类加载器。然后我评论spring-boot-devtools然后再次尝试这次没关系。那么如果仍然保留spring-boot-devtools,如何解决这个问题?

cLy*_*ric 7

Dozer 使用了错误的类加载器。
你可以解决它在你的资源文件夹中添加这个文件:
META-INF/spring-devtools.properties
with inside: restart.include.dozer
=/dozer-5.5.1.jar restart.include.dozer
-spring=/dozer-spring -5.5.1.jar(仅当您使用此 jar 时!)
来源:http : //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools-customizing-classload