spring-boot-devtools 不同ClassLoader引起的问题

在Spring Boot的文档spring-boot-devtools 部分,有如下的 描述:

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.

我遇到的问题是向上转型时出现ClassCastException,按说向上转型是不会出现这个问题的,于是想到可能是ClassLoader不一样导致的,打印了两个对象的ClassLoader,确实不同,一个RestartClassLoader,一个普通的AppClassLoader。

于是在Spring Boot DevTools的文档文档中找到上面的描述。

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

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

All property keys must be unique. As long as a property starts with
restart.include. or restart.exclude. it will be considered. All
META-INF/spring-devtools.properties from the classpath will be loaded.
You can package files inside your project, or in the libraries that
the project consumes.

最佳实践是将本项目中的各个模块的Jar包放到restart.include配置里面。

下面是一篇类似的文章,大家可以看下:使用Spring Boot本地启动,第三方包无法修改项目内静态常量

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈振阳

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值