在Filter bean类中使用一些bean?

Ant*_*ony 18 java spring servlets servlet-filters

在我的过滤器bean类中,我添加了一些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);
Run Code Online (Sandbox Code Playgroud)

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

你可以帮帮我吗 ?


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

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

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


Skaffman,

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

<bean id="FacebookOAuthHandler" class="com.xx.FacebookOAuthHandler" />
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
    <props>
       <prop key="/fbauth">FacebookOAuthHandler</prop>         
    </props>
   </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

和我的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;
}
Run Code Online (Sandbox Code Playgroud)

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

Mat*_* G. 33

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

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());
}
Run Code Online (Sandbox Code Playgroud)


ska*_*man 13

假设这Filter已经连接到你的web.xml,那么这不会起作用,因为它不是由Spring管理的,它由servlet容器管理.所以像自动装配这样的东西是行不通的.

如果要将servlet过滤器定义为Spring bean,则需要在webapp的根应用程序上下文中定义它(使用ContextLoaderListenerin web.xml),然后定义DelegatingFilterProxy哪个委托给Spring管理的bean来完成工作.

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


Dew*_*wfy 7

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

简而言之 - 您可以手动强制弹簧将@Autowire注释应用于您的过滤器:

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);
}
Run Code Online (Sandbox Code Playgroud)


Ser*_*sta 7

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

所以a DelegatingFilterProxy是一个特殊的过滤器,它知道root ApplicationContext并将其委托doFilter给bean.

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

<bean id=myFilter class="org.example.MyFilter ...>...</bean>
Run Code Online (Sandbox Code Playgroud)

或者在配置类中

@Bean
public MyFilter myFilter() {
    MyFilter myFilter = new MyFilter();
    //initialization ...
    return myFilter;
}
Run Code Online (Sandbox Code Playgroud)

在web.xml中,您只需声明一个DelegatingFilterProxy与bean相同的名称:

<filter>
    <filter-name>myFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>myFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Run Code Online (Sandbox Code Playgroud)

这样,正如myBean一个真正的bean一样,它可以正常注入带有@Autowired注释的其他bean ,并且它的doFilter方法将被调用DelegatingFilterProxy.正如你可以使用Spring init和destroy方法,在默认情况下initdestroy方法将不会被调用,除非您指定的"targetFilterLifecycle"过滤器的init-PARAM为"真".