Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A classloader proplem related to spring-boot-devtools

Background: Spring boot project, add goods and goods price list

Goods:
List<GoodsPrice> pricelist;

in controller first convert goodsForm to goods(by dozer), then save goods,after saving goods iterate goods price list to populate goodsId.

goods.getPriceList().forEach(p -> p.setGoodsId(goods.getId()));

When iterate goods price list, throw exception:

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]

Somebody remind me this exception related to classloader, and in eclipse debug mode, I outputed the GoodsPrice's ClassLoader:

sun.misc.Launcher$AppClassLoader@14dad5dc

and goods: org.springframework.boot.devtools.restart.classloader.RestartClassLoader@591c6338

Indeed exist diff classloader. Then I commented spring-boot-devtools then tried again this time it's ok. So if still retain spring-boot-devtools, how to solve this problem?

like image 696
zhuguowei Avatar asked Nov 27 '15 12:11

zhuguowei


People also ask

How do I use Devtools in spring boot?

DevTools stands for Developer Tool. The aim of the module is to try and improve the development time while working with the Spring Boot application. Spring Boot DevTools pick up the changes and restart the application. We can implement the DevTools in our project by adding the following dependency in the pom.

How do I remove Devtools in spring boot?

No, it's turned off automatically. From the Spring Boot reference documentation: Developer tools are automatically disabled when running a fully packaged application. If your application is launched using java -jar or if it's started using a special classloader, then it is considered a “production application”.

What is spring boot starter Devtools?

spring-boot-devtools module includes an embedded LiveReload server that is used to trigger a browser refresh when a resource is changed. For this to happen in the browser we need to install the LiveReload plugin one such implementation is Remote Live Reload for Chrome.


1 Answers

Dozer is using the wrong classloader.
You can solve it adding this file in your resource folder :
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 (only if you use this jar!)
sources : http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools-customizing-classload

like image 57
cLyric Avatar answered Sep 24 '22 22:09

cLyric