Spring cache 加 devtool热加载的小坑

错误:

异常:

Java.lang.ClassCastException

原因:

maven引入了 spring-boot-devtools。类加载器不同导致的问题。

解决方案:

https://github.com/spring-projects/spring-boot/issues/9444

手动设置类加载器

As explained in the documentation that I linked to above, DevTools uses two separate ClassLoaders: the app class loader and a restart class loader. The classes in your application (rediscache module) are loaded by the restart class loader so that they can be quickly reloaded as you make changes.

In your RedisCache class, you're creating a JdkSerializationRedisSerializer without specifying a ClassLoader. As a result, it uses the app class loader. This means that you end up with a User loaded by the app class loader being cast to the User type loaded by the restart class loader.

To fix the problem you need to specify a class loader when you create the serializer. For example:

RedisSerializer serializer = new JdkSerializationRedisSerializer(getClass().getClassLoader()); 
  

                            
                        
                    
                    
                    

你可能感兴趣的:(Java,spring,redis)