从servlet过滤器和标签访问Spring bean [英] Accessing Spring beans from servlet filters and tags

查看:147
本文介绍了从servlet过滤器和标签访问Spring bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用

WebApplicationContext springContext = 
    WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 

在Servlet的init方法中.

in the Servlet's init method.

我想知道servlet过滤器是否与WebApplicationContext等效? 另外,是否可以在标记类中访问Spring bean?

I was wondering is there an equivalent of the WebApplicationContext for servlet filters? Also, is it possible to access Spring beans in a tag class?

推荐答案

对于过滤器-使用Filter.init():

public void init(FilterConfig config) {
    WebApplicationContext springContext = 
        WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
}

对于标签-使用TagSupport.pageContext(请注意,在SimpleTagSupport中不可用):

For tags - use TagSupport.pageContext (note that it's not available in SimpleTagSupport):

WebApplicationContext springContext = 
    WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());

这篇关于从servlet过滤器和标签访问Spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆