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

查看:37
本文介绍了从 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():

For filters - use 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天全站免登陆