从 servlet 过滤器和标签访问 Spring bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2416671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing Spring beans from servlet filters and tags
提问by Damien
I can access Spring beans in my Servlets using
我可以使用在我的 Servlets 中访问 Spring bean
WebApplicationContext springContext =
WebApplicationContextUtils.getWebApplicationContext(getServletContext());
in the Servlet's initmethod.
在 Servlet 的init方法中。
I was wondering is there an equivalent of the WebApplicationContextfor servlet filters?
Also, is it possible to access Spring beans in a tag class?
我想知道是否有等效的WebApplicationContextfor servlet 过滤器?另外,是否可以在标签类中访问 Spring bean?
回答by axtavt
For filters - use Filter.init():
对于过滤器 - 使用Filter.init():
public void init(FilterConfig config) {
WebApplicationContext springContext =
WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
}
For tags - use TagSupport.pageContext(note that it's not available in SimpleTagSupport):
对于标签 - 使用TagSupport.pageContext(请注意,它在 中不可用SimpleTagSupport):
WebApplicationContext springContext =
WebApplicationContextUtils.getWebApplicationContext(pageContext.getServletContext());
回答by Alexis K
you can use a DelegatingFilterProxy as mentioned in Spring documentation: http://static.springsource.org/spring-security/site/docs/3.0.x/reference/security-filter-chain.html#delegating-filter-proxy
您可以使用 Spring 文档中提到的 DelegatingFilterProxy:http: //static.springsource.org/spring-security/site/docs/3.0.x/reference/security-filter-chain.html#delegating-filter-proxy
You just have to declare your real Filter bean with the same bean name as the filter-name declared in web.xml:
您只需要使用与 web.xml 中声明的过滤器名称相同的 bean 名称来声明真正的过滤器 bean:
web.xml:
网页.xml:
<filter>
<filter-name>SpringTestFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>SpringTestFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
applicationContext.xml:
应用上下文.xml:
<bean id="SpringTestFilter" class="com.company.app.servlet.SpringTestFilter" />
回答by Barry Knapp
There are a couple ways to get it
有几种方法可以得到它
WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(getFilterCongig().getServletContext());WebApplicationContext springContext = RequestContextUtils.getWebApplicationContext(servletRequest)
WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(getFilterCongig().getServletContext());WebApplicationContext springContext = RequestContextUtils.getWebApplicationContext(servletRequest)
then
然后
springContext.getBean("myBeanId");
回答by Bozho
You can put all your beans as request attributes by using the ContextEsposingHttpServletRequestwrapper.
您可以使用ContextEsposingHttpServletRequest包装器将所有 bean 作为请求属性。

