Fantastic_Clouds

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::
  @Override
    public void init(FilterConfig arg0) throws ServletException {
        ServletContext servletContext = arg0.getServletContext();
        XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(servletContext);
        if(cxt != null && cxt.getBean("serviceClass") != null && serviceClass == null) {
            serviceClass = (ServiceClass) cxt.getBean("serviceClass");
        }
    }

   首先在Filter实现过滤器类中声明将要注入的Service对象。在过滤器初始化init()方法中,通过FilterConfig文件获取上下文对象。调用Spring中WebApplicationContextUtils通过get方法获取XmlWebApplicationContext对象,调用getBean()即可。

posted on 2018-02-01 13:01  Fantastic_Clouds  阅读(730)  评论(0编辑  收藏  举报