java servlet过滤器中的弹簧注入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14765992/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:19:56  来源:igfitidea点击:

spring injection in servlet filter

javaspringspring-mvcspring-security

提问by Patan

I am trying to do spring injection to servlet filter.The filter is apart of the referenced jar files. so. I cannot change it as interceptor. In web.xml of my plugin project

我正在尝试对 servlet 过滤器进行 spring 注入。过滤器是引用的 jar 文件的一部分。所以。我无法将其更改为拦截器。在我的插件项目的 web.xml 中

<filter>
    <filter-name>CustomFilter</filter-name>    
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>    
    <init-param>    
        <param-name>someinitparam</param-name>    
        <param-value>value to it</param-value>    
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CustomFilter</filter-name>
    <url-pattern>/mywebservices/*</url-pattern>
</filter-mapping>

In spring.xml I will use like this

在 spring.xml 我会这样使用

<bean id="CustomFilter" class="com.abc.CustomFilter"></bean>

There are some filters are already configured in spring.xml as

spring.xml 中已经配置了一些过滤器

<bean id="filterChainProxy" class="org.acegisecurity.util.FilterChainProxy">
    <property name="filterInvocationDefinitionSource">
        <value>
            CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
            PATTERN_TYPE_APACHE_ANT
            /mywebservices/*=some existing filters
        </value>              
    </property>
</bean>

As I have already specified my url pattern in web.xml do I need to add again in filterChainProxy as

由于我已经在 web.xml 中指定了我的 url 模式,我是否需要在 filterChainProxy 中再次添加为

/mywebservices/**=CustomFilter, some existing filters

Will it work.

它会起作用吗。

Please suggest.

请建议。

回答by ckoidl

You can configure the filter like you did in your web.xml

您可以像在 web.xml 中一样配置过滤器

<filter>
   <filter-name>CustomFilter</filter-name>
   <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
   <filter-name>CustomFilter</filter-name>
   <url-pattern>/mywebservices/*</url-pattern>
</filter-mapping>

and then inject properties in the spring.xml

然后在 spring.xml 中注入属性

<bean id="CustomFilter" class="com.abc.CustomFilter">
   <property name="someParameter">
      <value>some value</value>
   </property>
</bean>

回答by Sujay Bawaskar

I think you can not inject the beans outside spring context and your servlet filter is outside the spring context. If you want a filter inside the context then I recommend go for spring web interceptors. These interceptors are within spring context and you can leverage spring container capabilities with these interceptors.

我认为您不能在 spring 上下文之外注入 bean,并且您的 servlet 过滤器在 spring 上下文之外。如果您想要在上下文中使用过滤器,那么我建议您使用 Spring Web 拦截器。这些拦截器在 spring 上下文中,您可以通过这些拦截器利用 spring 容器功能。