Search code examples
spring-mvcweb.xml

How does Spring wire a OncePerRequestFilter with its init-param values?


I've got a simple OncePerRequestFilter with a couple of init-param entries defined in my web.xml, but I don't know how to access them from within the filter. getFilterConfig() returns null. The init(FilterConfig) docs make it sound like it will do injection into bean properties based on the name of the init-param, e.g., a param named foo will have its value injected into a property on the filter named foo, but some logging in doFilterInternal seems to indicate that isn't happening.

Since I'm fairly new to modern Spring (last used it circa the 1.x days) and totally new to Spring MVC, I'm pretty sure I've just missed something obvious, but I can't see what it is. Thanks for any help.


Solution

  • The init-param values are mapped to properties of the filter it's self.

    public MyFilter extends OncePerRequestFilter {
    
        // the following should be called once the `GenericFilterBean` `init` method has run        
        public void setFoo(String foo){
             this.foo = foo;
        }
    }
    
    
    <filter>
         <init-param>
              <param-name>foo</param-name>
              <param-value>bar</param-value>
         </init-param>
    </filter>