zhu*_*wei 8 java classloader dozer spring-boot
我遇到了一个非常奇怪的异常,我不知道如何找到原因.
商业背景:添加商品并同时列出价格表,商品有差价水平用户的5个价格.
在控制器中,首先使用dozer将goodForm转换为商品,然后调用goodsService来保存商品.在货物服务保存货物后,遍历货物价格表并填充货物ID到货物价格,
GoodsForm:
@Mapping("priceList")
List<GoodsPriceForm> goodsPriceFormList;
Goods:
List<GoodsPrice> priceList;
Controller:
Goods goods = BeanMapper.map(goodsForm, Goods.class);
goodsService.saveGoods(adminId, goods);
GoodsService:
goodsDao.save(goods);
goods.getPriceList().forEach(p -> p.setGoodsId(goods.getId()));
goodsPriceDao.save(goods.getPriceList());
Run Code Online (Sandbox Code Playgroud)
但它抛出异常:
2015-11-27 17:10:57,042 [http-nio-8081-exec-8] ERROR o.a.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.ClassCastException: com.foo.goods.model.GoodsPrice cannot be cast to com.foo.goods.model.GoodsPrice] with root cause
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)
这个错误信息让我感到很困惑.另外我写一个单元测试想重复这个,但失败了.
GoodsForm form = new GoodsForm();
form.setGoodsPriceFormList(Lists.newArrayList(new GoodsPriceForm((byte) 1, BigDecimal.valueOf(10)),
new GoodsPriceForm((byte) 2, BigDecimal.valueOf(9)),
new GoodsPriceForm((byte) 3, BigDecimal.valueOf(8))));
Goods goods = BeanMapper.map(form, Goods.class);
goods.getPriceList().forEach(p -> p.setGoodsId(goods.getId()));
Run Code Online (Sandbox Code Playgroud)
运行此单元测试,执行正常.那么为什么在真实的网络情况下(Spring boot + Jpa)它失败了,但在单元测试情况下它没问题呢?
Controller:
System.out.println("PriceList: " + goods.getPriceList().getClass().getClassLoader());//PriceList: null
System.out.println(goods.getPriceList().get(0).getClass().getClassLoader()); //java.lang.ClassCastException: com.foo.goods.model.GoodsPrice cannot be cast to com.foo.goods.model.GoodsPrice
Run Code Online (Sandbox Code Playgroud)
如果我生成了一个打包的jar,那么执行这个jar
java -jar target/myapp.jar
Run Code Online (Sandbox Code Playgroud)
在这种情况下没有上述例外.
我在pom.xml中评论了spring-boot-devtools,然后启动了应用程序,没有异常.
zhu*_*wei 11
默认情况下,将使用"restart"类加载器加载IDE中的任何打开项目,并使用"base"类加载器加载任何常规.jar文件.如果您处理多模块项目,而不是将每个模块导入IDE,则可能需要自定义项目.为此,您可以创建META-INF/spring-devtools.properties文件.
spring-devtools.properties文件可以包含restart.exclude.并重新启动.包含.前缀属性.include元素是应该被提升到"restart"类加载器中的项,而exclude元素是应该被下推到"base"类加载器中的项.属性的值是将应用于类路径的正则表达式模式.
我的解决方案:放入META-INF/spring-devtools.properties资源文件夹,并添加此内容
restart.include.dozer=/dozer-5.5.1.jar
Run Code Online (Sandbox Code Playgroud)
请参阅:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-devtools-customizing-classload
| 归档时间: |
|
| 查看次数: |
3139 次 |
| 最近记录: |