国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

香港云服务器
服務器之家 - 編程語言 - Java教程 - 解決SpringBoot使用devtools導致的類型轉換異常問題

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

2020-08-25 00:15試水流連 Java教程

這篇文章主要介紹了解決SpringBoot使用devtools導致的類型轉換異常問題,具有很好的參考價值,希望對大家有所幫助。 一起跟隨小編過來看看吧

問題:

最近在使用新框架SpringBoot + shiro + spring-data-jpa時,為了體驗下spring自帶的熱部署工具的便捷,于是引入了

?
1
2
3
4
5
6
7
8
<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

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

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

并打包,在springboot項目中引入

?
1
2
3
4
5
6
7
8
9
10
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 這個包是我自己創建的序列化以及反序列化工具包 -->
<dependency>
  <groupId>com.example</groupId>
  <artifactId>devtools-serialization</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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引導類加載器加載的,因此不會產生任何問題,

但是如果改成下面這樣

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//...
 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信息會發現分別為

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

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

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

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

AppClassLoader

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

在resources下創建META-INF/spring-devtools.properties

如圖:

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

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

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

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

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

restart.include.*

value 為一個正則表達式

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

沒有異常產生

控制臺輸出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這個模塊來使應用支持熱部署,可以提高開發者的開發效率,無需手動重啟Spring Boot應用就能實現自動加載,之前寫了一篇可以自動加載springboot靜態文件的,這次的只需要在原來的基礎上再加一些配置即可實現springboot工程的熱部署,步驟如下:

1、pom文件增加依賴:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<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文件中添加配置使其生效:

?
1
2
3
4
5
6
7
8
# devtools
debug: true
spring:
 devtools:
  restart:
   enabled: true #設置開啟熱部署
 freemarker:
  cache: false  #頁面不加載緩存,修改即時生效

3、快捷鍵:Ctrl+Alt+S

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

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

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

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

原文鏈接:https://blog.csdn.net/m0_38043362/article/details/78064539

延伸 · 閱讀

精彩推薦
1244
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40 Weibo Article 41 Weibo Article 42
主站蜘蛛池模板: 亚洲一区二区三区免费观看 | 欧美视频精品 | 久久伊人官网 | 观看av| 欧美成人一区二区三区 | 91精品国产综合久久久久久丝袜 | 黄色av免费 | 超碰一区二区三区 | 国产福利在线 | 国产精品视频一区二区三区四 | 国产免费久久 | 免费观看www7722午夜电影 | 国产精品自拍视频 | 韩国理论电影在线 | 日韩av一级在线观看 | 国产麻豆乱码精品一区二区三区 | 自拍偷拍在线视频 | 成人国产精品免费观看 | 寡妇高潮免费视频一区二区三区 | 自拍av在线 | 日韩欧美一区二区三区免费观看 | 日韩在线成人 | 国产精品视频网站 | 亚洲精选一区二区 | 一级毛毛片 | 国产999精品久久久久久 | 亚洲大奶网 | 黄网免费看 | 久久三区 | 日韩成人免费在线 | 精品视频久久 | 国产日韩欧美高清 | 69久久夜色精品国产69乱www | a毛片| 综合色播| 毛片网站在线观看 | 成人精品久久久 | 日韩欧美精品在线 | 久久久国产视频 | 成人av一区二区三区 | 亚洲不卡高清视频 |