如何在 spring 应用程序的 servlet 过滤器中自动装配 bean?

how to autowire bean in the servlet filters in spring application?

我有一个spring-boot申请。

我的项目中没有 ApplicationContext.xmlweb.xml 文件。我更愿意避免使用它们,并在 Java 代码中配置所有内容。

我已阅读以下 post关于 servlet 过滤器中的 bean 注入的文章。

  1. How can I get a Spring bean in a servlet filter?

  2. http://www.deadcoderising.com/2015-05-04-dependency-injection-into-filters-using-delegatingfilterproxy/

  3. spring injection in servlet filter

看完之后,我开始使用DelegatingFilterProxy

我的问题是如何将 bean autowire 放入过滤器并避免使用 xml 文件,尤其是对于 DelegatingFilterProxy 配置。

github.

中托管的第二个 post 中提供了截取的代码
public class AuditHandler {

    public void auditRequest(String appName, ServletRequest request) {
        System.out.println(appName + ": Received request from " + request.getRemoteAddr() );
    }
}

public class AuditFilter implements Filter {

    private final AuditHandler auditHandler;
    private String appName;

    public AuditFilter(AuditHandler auditHandler) {
        this.auditHandler = auditHandler;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        auditHandler.auditRequest(appName, request);
        chain.doFilter(request, response);
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        appName = filterConfig.getInitParameter("appName");
    }

    public void destroy() {}
}

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="auditHandler" class="com.deadcoderising.AuditHandler">
    </bean>

    <bean id="auditFilter" class="com.deadcoderising.AuditFilter">
        <constructor-arg ref="auditHandler"/>
    </bean>
</beans>

web.xml

<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         metadata-complete="true">

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext*.xml</param-value>
    </context-param>


    <filter>
        <filter-name>auditFilter</filter-name>
        <filter-class>
            org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>appName</param-name>
            <param-value>di-example</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>auditFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

您应该将 FilterRegistrationBean 添加到您的主应用程序 class(class 用 @SpringBootApplication 注释)并让 Spring 提供 AuditHandler:

@Bean
@Autowired
public FilterRegistrationBean auditFilterRegistration(AuditHandler handler) {
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
    filterRegistrationBean.setFilter(new AuditFilter(handler));
    filterRegistrationBean.setOrder(3); // ordering in the filter chain
    return filterRegistrationBean;
}

如果这不起作用(例如,您的 AuditHandler 实现未正确注释或它不在默认包扫描路径上),您可以指示 Spring 提供它(也在您的 @SpringBootApplication注解class):

@Bean
public AuditHandler auditHandler() {
    return new AuditHandlerImplementation();
}