adolph_1 2019-11-13 09:53 采纳率: 16.7%
浏览 1424
已采纳

java springcloud项目在微服务中,在filter过滤器使用RedisTemplate,报错,请大佬帮我看下?

java.lang.ClassCastException: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext cannot be cast to org.springframework.web.context.support.XmlWebApplicationContext
at com.knowind.inteBPM.config.AddAuthFilter.init(AddAuthFilter.java:30) ~[classes/:na]
at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) ~[tomcat-embed-core-8.5.14.jar:8.5.14]
at org.apache.catalina.core.ApplicationFilterConfig.(ApplicationFilterConfig.java:109) ~[tomcat-embed-core-8.5.14.jar:8.5.14]
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4590) [tomcat-embed-core-8.5.14.jar:8.5.14]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5233) [tomcat-embed-core-8.5.14.jar:8.5.14]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.14.jar:8.5.14]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419) [tomcat-embed-core-8.5.14.jar:8.5.14]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409) [tomcat-embed-core-8.5.14.jar:8.5.14]
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) [na:1.8.0_192]
at java.util.concurrent.FutureTask.run(FutureTask.java) [na:1.8.0_192]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_192]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_192]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_192]
在filter的init方法中加入了下面代码,启动就报错了

public class AddAuthFilter implements Filter {

    RedisTemplate redisTemplate;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        ServletContext sc = filterConfig.getServletContext();
        XmlWebApplicationContext ctx = (XmlWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(sc);
                if (ctx != null && ctx.getBean("redisTemplate") != null && redisTemplate == null) {
            redisTemplate = (RedisTemplate) ctx.getBean("redisTemplate");
        }
    }
    }
  • 写回答

1条回答 默认 最新

  • Kevin.Y.K 2019-11-13 10:17
    关注


    XmlWebApplicationContext ctx = (XmlWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(sc);
    改成
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥33 集成BiLSTM模型和GCN模型时,前模型的输出不匹配后模型的输入
  • ¥15 关于液体厚度提取,和铺展轨迹数据化处理
  • ¥20 对方微信是wxid如何添加成好友?或者转换成微信号?
  • ¥15 yolov5实验数据好,但是自己拍摄的照片检测结果差
  • ¥15 做过dali2.0认证的技术,
  • ¥15 一道python难题
  • ¥15 matlab 免疫算法
  • ¥50 可视化界面构建,实现信息模块功能
  • ¥15 基于bert-base-chinese的中文文本处理
  • ¥15 高价求抖音抓包技术,抖音抓包无网络问题