在Filter bean类中使用一些bean?

在我的filterbean类中,我添加了一些bean依赖项(使用@Autowired注释)。 但是在doFilter()方法中,我的所有依赖bean都是null …

 public class FacebookOAuth implements Filter { @Autowired private BusinessLogger logger; @Autowired private IUserSessionInfo userSessionInfo; @Autowired private FacebookOAuthHelper oAuthHelper; public void init(FilterConfig fc) throws ServletException { // Nothing to do } public void doFilter(ServletRequest sr, ServletResponse sr1, FilterChain fc) throws IOException, ServletException { // HttpServletRequest req = (HttpServletRequest)sr; HttpServletResponse res = (HttpServletResponse) sr1; String code = sr.getParameter("code"); if (StringUtil.isNotBlankStr(code)) { String authURL = this.oAuthHelper.getAuthURL(code); 

this.oAuthHelper等于null(和其他依赖bean)…

你可以帮帮我吗 ?


实际上我并没有在服务器端使用MVC概念(Spring)。 对于我的客户端,我使用Flex技术,BlazeDS servlet与我的服务器进行通信。

所以,这就是我使用Filter bean概念的原因。

那么,我如何处理我的Filter bean中的会话bean概念?


Skaffman,

我实现了你的想法,所以我更新了我的application.xml:

     FacebookOAuthHandler    

和我的FacebookOAuthHandler类:

 public class FacebookOAuthHandler extends AbstractController { @Autowired private BusinessLogger logger; @Autowired private IUserSessionInfo userSessionInfo; @Autowired private FacebookOAuthHelper oAuthHelper; @Override protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO return null; } 

但是,当我的URL为: http://xx.xx.xx.xx/MyApp/fbauth时,永远不会调用此方法handleRequestInternal

我遇到了同样的问题,我的第一个想法是手动强制Spring将@Autowired注释应用于此处提出的filter

http://forum.springsource.org/showthread.php?60983-Autowiring-the-servlet-filter

但我不喜欢在我的Java类中硬编码bean名称的想法。

我找到了一种更清洁的方式:

 public void init(FilterConfig filterConfig) throws ServletException { SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, filterConfig.getServletContext()); } 

假设这个Filter在你的web.xml连接起来,那么这不会起作用,因为它不是由Spring管理的,而是由servlet容器管理的。 所以像自动assembly这样的东西是行不通的。

如果要将servletfilter定义为Spring bean,则需要在webapp的根应用程序上下文中定义它(使用web.xml中的ContextLoaderListener ),然后定义委托给Spring管理的bean 的DelegatingFilterProxy来执行工作。

但是,你真的需要一个servletfilter吗? 我所知道的Facebook auth的东西,这可以通过Spring HandlerInterceptor轻松完成 。 这将比委托filter少得多的配置工作。

请看spring网站上的这个答案: http : //forum.springsource.org/showthread.php?60983-Autowiring-the-servlet-filter

简而言之 – 您可以手动强制弹簧将@Autowire注释应用于filter:

 public void init(FilterConfig filterConfig) throws ServletException { ServletContext servletContext = filterConfig.getServletContext(); WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext); AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext.getAutowireCapableBeanFactory(); autowireCapableBeanFactory.configureBean(this, BEAN_NAME); } 

我知道这是一个现在很老的问题,但是在当前的响应中没有使用DelegatingFilterProxy例子,并且最近一个问这样一个例子的问题被标记为这个问题的副本。

所以DelegatingFilterProxy是一个特殊的filter,它知道根ApplicationContext并将其doFilter委托给bean。

示例: MyFilter是一个实现Filter的类,myFilter是一个spring bean

 
		      	

我在使用自动assembly访问filter类中的服务类bean时获得空指针。 我搜索了100多个链接,但无法找到解决方案。 我正在使用spring引导应用程序和配置,这是在filter类中获取bean所需的:

FilterConfig.java,提供应用程序上下文Object。

 @Component public class FilterConfig implements ApplicationContextAware{ private static ApplicationContext context; public static ApplicationContext getApplicationContext() { return context; } public static  T getBean(String name,Class aClass){ return context.getBean(name,aClass); } @Override public void setApplicationContext(ApplicationContext ctx) throws BeansException { context = ctx; } } 

在Filter类中,我使用了这个:

  UserService userService =FilterConfig.getBean("UserService", UserService.class); 

UserService这是bean中提到的名称

  @Service("UserService") public class UserServiceImpl implements UserService { ...} 

Spring Boot的主类中没有配置:SpringBootServletInitializer