shiro cache management error java.lang.ClassCastException time: com.px.myshiro.domain.User can not be cast to com.px.myshir

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_42614414/article/details/98501653

First, abnormal

java.lang.ClassCastException: com.px.myshiro.domain.User cannot be cast to com.px.myshiro.domain.User

problem

user不能转换为user,分明就是同一个user,怎么可能不能转换呢?需要转换吗?

analysis

这主要发生在缓存服务时。主要原因是:**同一个类被不同的加载器加载**

当对象序列化到缓存中时,我们假设此时的类加载器为C1
当代码发生改变时,会有另一个加载器,我们称它为C2
当你调用缓存中的方法时,缓存抽象会在缓存中找到一个条目,并从缓存中进行反序列化,如果没有考虑上下文的类加载器,那么,这个对象就会被赋予错误的加载器。

Solution one : If you did not read what I wrote, or you do not need hot deployment, you can put spring-boot-devtools rely comment in the pom.xml file;

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>

Solution two : PropertyUtils.copyProperties (to, from);

public static UserPo getUser() {
      Object object = getSubjct().getPrincipal();
      UserPo userPo = new UserPo();
      try {
          PropertyUtils.copyProperties(userPo,object);
      } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
          e.printStackTrace();
      }
      return userPo;
}

Second, how to set up the hot deployment in IDEA

Adding the pom.xml

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
   <optional>true</optional>
</dependency>

<plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
   <configuration>
      <fork>true</fork>
   </configuration>
</plugin>    

In Eclipse default automatically compiled, but IDEA requires installation, the following steps:
Here Insert Picture Description
if not, Shift + Ctrl + Alt + / , Select Registry
Here Insert Picture Description
then go, as shown in option found, tick
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_42614414/article/details/98501653