从OncePerRequestFilter访问spring bean [英] Access to spring beans from OncePerRequestFilter
问题描述
我想从OncePerRequestFilter类访问一个spring组件,但是访问服务时得到的是空指针,我认为原因是配置. 我认为由于配置,过滤器在spring调度程序servlet之前被调用.有什么好办法可以做到这一点,请提出建议.
I want to access a spring component from OncePerRequestFilter class, but I am getting null pointer when I access service, and I think the reason for that is the configuration. I think the filter is getting called before the spring dispatcher servlet due to the configuration. Any good way to get this done, suggestions please.
<servlet>
<servlet-name>SpringDispatcherServer</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/springConfig.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringDispatcherServer</servlet-name>
<url-pattern>/test/api/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>AuthCheck</filter-name>
<filter-class>org.test.util.AuthCheckFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthCheck</filter-name>
<url-pattern>/test/api/*</url-pattern>
</filter-mapping>`
public class AuthCheckFilter extends OncePerRequestFilter
{
@Autowired
private AuthCheckService authCheckService;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
log.info("authCheckService"+authCheckService);
,并且记录器为"authCheckService"输出null
and the logger prints null for "authCheckService"
推荐答案
您的过滤器是在Spring容器外部配置的.因此,不会注入您的@Autowired依赖项.
You filter is being configured outside the Spring container. Hence your @Autowired dependency is not injected.
要使您的Filter bean由Spring管理,而又不建议通过使用SpringBeanAutowiringSupport将其紧密耦合到Spring基础结构,则可以使用DelegatingFilterProxy抽象
To have your Filter bean managed by Spring without also tightly coupling it to Spring infrastructure through the use of SpringBeanAutowiringSupport suggested , you can use the DelegatingFilterProxy abstraction
在您的应用程序上下文中将AuthCheckFilter过滤器定义为bean,例如
Define AuthCheckFilter filter as a bean in your application context e.g
@Bean
public Filter authCheckFilter(){
AuthCheckFilter filter = new AuthCheckFilter();
//supply dependencies
return filter;
}
然后在web.xml中将过滤器类指定为org.springframework.web.filter.DelegatingFilterProxy,并且过滤器名称必须与上下文中的authCheckFilter bean名称匹配
Then in your web.xml specify your filter with filter-class as org.springframework.web.filter.DelegatingFilterProxy and the filter name must match the authCheckFilter bean name in the context
在运行时,DelegatingFilterProxy过滤器将委派给上下文中名称为AuthCheckFilter的完全配置的bean(必须为过滤器)
At runtime DelegatingFilterProxy filter will delegate to a fully configured bean with the name authCheckFilter in the context (which must be a Filter)
<filter>
<filter-name>authFilterCheck</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>authCheckFilter</filter-name>
<url-pattern>/test/api/*</url-pattern>
</filter-mapping>
通过此设置,您不必担心筛选器,根上下文或servlet的生命周期
With this setup you wont have to worry about the lifecyles of your filter , root context or servlet
这篇关于从OncePerRequestFilter访问spring bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!