Spring Boot 分離打包踩坑-無法加載class(ClassNotFoundException)-shiro引起

正常打包項(xiàng)目正常,分離打包項(xiàng)目報(bào)空指針

2018-07-13 14:32:43,418[ERROR][http-nio-1314-exec-1] org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/template].[dispatcherServlet][182] Servlet.service() for servlet [dispatcherServlet] in context with path [/template] threw exception [Request pro
cessing failed; nested exception is org.springframework.data.redis.serializer.SerializationException: Cannot deserialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to deserialize payload. Is the byte ar
ray a result of corresponding serialization for DefaultDeserializer?; nested exception is org.springframework.core.NestedIOException: Failed to deserialize object type; nested exception is java.lang.ClassNotFoundException: site.yuyanjia.template.common.model.We
bUserDO] with root cause
java.lang.ClassNotFoundException: site.yuyanjia.template.common.model.WebUserDO
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[?:1.8.0_40]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_40]
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[?:1.8.0_40]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_40]
        at java.lang.Class.forName0(Native Method) ~[?:1.8.0_40]
        at java.lang.Class.forName(Class.java:348) ~[?:1.8.0_40]

我碰到的問題是因?yàn)轫?xiàng)目中用到了 redis的 JdkSerializationRedisSerializer序列化
JdkSerializationRedisSerializer使用的 ClassLoader不同導(dǎo)致找不到class

舊代碼

    /**
     * redis模板
     *
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate redisTemplateWithJdk(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory);

        stringRedisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        stringRedisTemplate.afterPropertiesSet();
        return stringRedisTemplate;
    }

新代碼

    /**
     * redis模板
     *
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate redisTemplateWithJdk(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(redisConnectionFactory);
        /**
         * SpringBoot擴(kuò)展了ClassLoader,進(jìn)行分離打包的時(shí)候,使用到JdkSerializationRedisSerializer的地方
         * 會(huì)因?yàn)镃lassLoader的不同導(dǎo)致加載不到Class
         * 指定使用項(xiàng)目的ClassLoader
         *
         * JdkSerializationRedisSerializer默認(rèn)使用{@link sun.misc.Launcher.AppClassLoader}
         * SpringBoot默認(rèn)使用{@link org.springframework.boot.loader.LaunchedURLClassLoader}
         */
        ClassLoader classLoader = this.getClass().getClassLoader();
        stringRedisTemplate.setValueSerializer(new JdkSerializationRedisSerializer(classLoader));
        stringRedisTemplate.afterPropertiesSet();
        return stringRedisTemplate;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容