亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

解決SpringBoot使用devtools導致的類型轉換異常問題

 更新時間:2020年08月24日 09:49:55   作者:試水流連  
這篇文章主要介紹了解決SpringBoot使用devtools導致的類型轉換異常問題,具有很好的參考價值,希望對大家有所幫助。 一起跟隨小編過來看看吧

問題:

最近在使用新框架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實現(xiàn)的,然后再使用Session存取屬性時,發(fā)現(xiàn)存進去的屬性,再取出來后,就會出現(xiàn)類型轉換異常ClassCastException

分析:

然后自己寫了一大推單元測試模擬就是沒問題,后來突然意識到會不會是因為ClassLoader不同導致的類型轉換異常呢,然后注意了下項目啟動時加載項目中的類使用的加載器都是

org.springframework.boot.devtools.restart.classloader.RestartClassLoader

而從shiro session 取出來的對象(從redis中取出經(jīng)過反序列化)的類加載器都是

sun.misc.Launcher.AppClassLoader

很明顯會導致類型轉換異常,原來Spring的dev-tools為了實現(xiàn)重新裝載class自己實現(xiàn)了一個類加載器,來加載項目中會改變的類,方便重啟時將新改動的內容更新進來,其實其中官方文檔中是有做說明的:

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目錄下面創(chuàng)建META-INF文件夾,然后創(chuàng)建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

針對方案一作一個詳細的案例進行分析說明,以及解決問題

首先準備一個jar包,里面包含序列化以及反序列化的功能。

并打包,在springboot項目中引入

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 這個包是我自己創(chuàng)建的序列化以及反序列化工具包 -->
<dependency>
  <groupId>com.example</groupId>
  <artifactId>devtools-serialization</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

簡單的配置下springboot項目,并模擬使用jar中的序列化工具類進行處理對象如下

@SpringBootApplication
public class PortalApplication {
  public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(PortalApplication.class, args);
    DemoBean demoBean = new DemoBean();
    SerializationUtils.serialize(demoBean);
    Object deserialize = SerializationUtils.deserialize();
    System.out.println(PortalApplication.class.getClassLoader());
    //這里對象引用是Object類型
    System.out.println(deserialize);
    System.out.println(deserialize.getClass().getClassLoader());
    context.getBeanFactory().destroySingletons();
  }
}

如上,是不會報錯的,因為Object是bootstrap引導類加載器加載的,因此不會產(chǎn)生任何問題,

但是如果改成下面這樣

//...
 public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(PortalApplication.class, args);
    DemoBean demoBean = new DemoBean();
    SerializationUtils.serialize(demoBean);
    Object deserialize = SerializationUtils.deserialize();
    System.out.println(PortalApplication.class.getClassLoader());
    //注意這里進行了一次類型強轉
    System.out.println((DemoBean)deserialize);
    System.out.println(deserialize.getClass().getClassLoader());
    context.getBeanFactory().destroySingletons();
  }
  //...

結果是會拋出:

Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) Caused by: java.lang.ClassCastException: com.sample.serial.DemoBean cannot be cast to com.sample.serial.DemoBean at com.sample.PortalApplication.main(PortalApplication.java:27) ... 5 more

而觀察上面輸出的ClassLoader信息會發(fā)現(xiàn)分別為

org.springframework.boot.devtools.restart.classloader.RestartClassLoader@63059d5a sun.misc.Launcher$AppClassLoader@18b4aac2

這就是為什么會明明沒問題,卻仍然拋了個ClassCastException的根源所在。

那么如何解決這個問題呢?

將輸出的ClassLoader信息保持一致即可,要么都是RestartClassLoader要么都是

AppClassLoader

這里參考spring官方文檔給出的配置方法進行處理。

在resources下創(chuàng)建META-INF/spring-devtools.properties

如圖:

下一步在spring-devtools.properties添加配置

restart.include.projectcommon=/devtools-serialization-[\\w.-]+.jar

注意這里我需要包含的jar包名稱為devtools-serialization-1.0-SNAPSHOT.jar

配置的key以restart.include.開頭即可

restart.include.*

value 為一個正則表達式

下面再次運行程序查看效果:

沒有異常產(chǎn)生

控制臺輸出classLoader信息為

org.springframework.boot.devtools.restart.classloader.RestartClassLoader@1d9fbdd4 DemoBean{age=null, name='null'} org.springframework.boot.devtools.restart.classloader.RestartClassLoader@1d9fbdd4

問題完美解決。

補充知識:Springboot+devtools配置熱部署

Spring Boot提供了spring-boot-devtools這個模塊來使應用支持熱部署,可以提高開發(fā)者的開發(fā)效率,無需手動重啟Spring Boot應用就能實現(xiàn)自動加載,之前寫了一篇可以自動加載springboot靜態(tài)文件的,這次的只需要在原來的基礎上再加一些配置即可實現(xiàn)springboot工程的熱部署,步驟如下:

1、pom文件增加依賴:

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
  </dependency>
</dependencies>
 
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <fork>true</fork> <!--重要-->
      </configuration>
    </plugin>
  </plugins>
</build>

2、yml文件中添加配置使其生效:

# devtools
debug: true
spring:
 devtools:
  restart:
   enabled: true #設置開啟熱部署
 freemarker:
  cache: false  #頁面不加載緩存,修改即時生效

3、快捷鍵:Ctrl+Alt+S

4、快捷鍵:Ctrl+Shift+A,輸入Registry,點擊進入勾選:

以上這篇解決SpringBoot使用devtools導致的類型轉換異常問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Java編程中的構造函數(shù)詳細介紹

    Java編程中的構造函數(shù)詳細介紹

    這篇文章主要介紹了Java編程中的構造函數(shù)詳細介紹,介紹了其概念,格式,與其他函數(shù)的區(qū)別,作用等相關內容,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • Java調用Oracle存儲過程詳解

    Java調用Oracle存儲過程詳解

    這篇文章主要介紹了Java調用Oracle存儲過程詳解的相關資料,需要的朋友可以參考下
    2017-02-02
  • mybatis中大批量數(shù)據(jù)插入解析

    mybatis中大批量數(shù)據(jù)插入解析

    這篇文章主要介紹了mybatis中大批量數(shù)據(jù)插入解析,使用Mybatis框架批量插入的3種方法,分別是多次調用insert方法、foreach標簽、batch模式,本文來詳細說明一下,需要的朋友可以參考下
    2024-01-01
  • java開源項目jeecgboot的超詳細解析

    java開源項目jeecgboot的超詳細解析

    JeecgBoot是一款基于BPM的低代碼平臺,下面這篇文章主要給大家介紹了關于java開源項目jeecgboot的相關資料,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-10-10
  • jpa介紹以及在spring boot中使用詳解

    jpa介紹以及在spring boot中使用詳解

    最近在項目中使用了一下jpa,發(fā)現(xiàn)還是挺好用的。這里就來講一下jpa以及在spring boot中的使用。在這里我們先來了解一下jpa,希望能給你帶來幫助
    2021-08-08
  • Java實現(xiàn)二叉搜索樹的插入、刪除功能

    Java實現(xiàn)二叉搜索樹的插入、刪除功能

    這篇文章主要介紹了Java實現(xiàn)二叉搜索樹的插入、刪除,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • HashMap和List遍歷方法及如何遍歷刪除元素總結

    HashMap和List遍歷方法及如何遍歷刪除元素總結

    在本篇文章中小編給大家分享了關于HashMap和List遍歷方法及如何遍歷刪除元素知識點總結,需要的朋友們參考下。
    2019-05-05
  • J2EE Servlet上傳文件到服務器并相應顯示功能的實現(xiàn)代碼

    J2EE Servlet上傳文件到服務器并相應顯示功能的實現(xiàn)代碼

    這篇文章主要介紹了J2EE Servlet上傳文件到服務器,并相應顯示,在文中上傳方式使用的是post不能使用get,具體實例代碼大家參考下本文
    2018-07-07
  • 使用maven運行Java Main的三種方法解析

    使用maven運行Java Main的三種方法解析

    這篇文章主要介紹了使用maven運行Java Main的三種方式的相關內容,具有一定參考價值,需要的朋友可以了解下。
    2017-10-10
  • Java Collection集合遍歷運行代碼實例

    Java Collection集合遍歷運行代碼實例

    這篇文章主要介紹了Java Collection集合遍歷運行代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04

最新評論