处理监听器注入springBean问题

由于监听器是在web.xml上配置的,因此在代码中注入Bean,并不受spring框架的支持,因此须要加入如下代码。java

VistedAnalyticsService vistedAnalyticsService = (VistedAnalyticsService) this
                    .getObjectFromApplication(sessionEvent.getSession().getServletContext(), "vistedAnalyticsService");


  

/**
     * 经过WebApplicationContextUtils 获得Spring容器的实例。根据bean的名称返回bean的实例。
     *
     * @param servletContext
     *            :ServletContext上下文。
     * @param beanName
     *            :要取得的Spring容器中Bean的名称。
     * @return 返回Bean的实例。
     */
    private Object getObjectFromApplication(ServletContext servletContext, String beanName) {
        // 经过WebApplicationContextUtils 获得Spring容器的实例。
        ApplicationContext application = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        // 返回Bean的实例。
        return application.getBean(beanName);
    }
相关文章
相关标签/搜索