SpringBoot使用devtools导致的类型转换异常
2017-09-22 16:42
405 查看
问题:
最近在使用新框架SpringBoot + shiro + spring-data-jpa时,为了体验下spring自带的热部署工具的便捷,于是引入了<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 --> <optional>true</optional> </dependency>
在起初并没遇到什么问题,当使用shiro的session管理,而且用的sessionDao是redis实现的,然后再使用Session存取属性时,发现存进去的属性,再取出来后,就会出现类型转换异常ClassCastException
分析:
然后自己写了一大推单元测试模拟就是没问题,后来突然意识到会不会是因为ClassLoader不同导致的类型转换异常呢,然后注意了下项目启动时加载项目中的类使用的加载器都是org.springframework.boot.devtools.restart.classloader.RestartClassLoader
而从shiro session 取出来的对象(从redis中取出经过反序列化)的类加载器都是
sun.misc.Launcher.AppClassLoader
很明显会导致类型转换异常,原来Spring的dev-tools为了实现重新装载class自己实现了一个类加载器,来加载项目中会改变的类,方便重启时将新改动的内容更新进来,其实其中官方文档中是有做说明的:
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.
解决:
方案一、解决方案就是在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.
方案二、不使用spring-boot-devtools
相关文章推荐
- SpringBoot使用devtools导致的类型转换异常
- 使用spring-boot-devtools导致在Eclipse下的UnsatisfiedLinkError问题
- vs2012中使用Spring.NET报错:Spring.Context.Support.ContextRegistry 的类型初始值设定项引发异常
- 关于使用BeanUtils.copyProperties拷贝时,时间类型为空时转换异常
- 解决spring使用动态代理类型转换失败的问题--java.lang.ClassCastException: com.sun.proxy.$Proxy$ cannot be cast to ...
- spring参数类型异常输出,SpringMvc参数类型转换错误输出
- 解决spring使用动态代理类型转换失败的问题--java.lang.ClassCastException: com.sun.proxy.$Proxy$ cannot be cast to ...
- 使用HibernateTemplate对数据库查询count时出现的类型转换的异常
- 68. 使用thymeleaf报异常:Not Found, status=404【从零开始学Spring Boot】
- spring-boot 1.3 以上版本使用spring-boot-devtools进行版本升级
- Spring boot 中 使用 Jackson 将null 字段转换为空字符串写法
- 类型转换装箱和拆箱导致的空指针异常
- Spring.Net -- 如何使用用户自定义类型转换器 (Custom Type Converter )
- Intellij IDEA 使用Spring-boot-devTools无效解决办法
- spring参数类型异常输出(二), SpringMvc参数类型转换错误输出(二)
- maven工程使用spring-boot-devtools进行热部署,更改代码避免重启web容器
- [原创]Spring Boot + Mybatis 简易使用指南(二)多参数方法支持 与 Joda DateTime类型支持
- Spring 3.1.1使用Mvc配置全局日期转换器,处理日期转换异常
- SSH(Spring+SpringMVC+hibernate+Maven) 项目 使用jackson报转换异常
- Spring 3.1.1使用Mvc配置全局日期转换器,处理日期转换异常