1

I have some external library Filters I want to include in my Java/Spring webapp. So I don't have access to change the source of the Filters themselves. The Filters have configurable options based on the FilterConfig. Normally these can be set in the web.xml as

<filter>
    <filter-name>foo</filter-name>
    <filter-class>com.acme.FooFilter</filter-class>
    <init-param>
        <param-name>fooParam</param-name>
        <param-value>bar</param-value>
    </init-param>
 </filter>

However, I don't want a static value for the param. Rather I want to use a environment specific variable. Normally in Spring I would use environmental specific property files to supply these kinds of values. How do I get that into the web.xml without having separate web.xml files? Can I provide a custom FilterConfig?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
ryber
  • 4,537
  • 2
  • 26
  • 50
  • Configure your `FooFilter` in the `ApplicationContext` assuming that it also has a `setFooParam` and instead of your filter add a `DelegatingFilterProxy` to your web.xml. – M. Deinum Sep 14 '15 at 14:02
  • Unfortunately no setters, it's only available through the FilterConfig, the classes aren't final so I could extend them and make my own but that seems heavy handed and ugly. – ryber Sep 14 '15 at 17:26
  • Using the DelegatingFilterProxy and a FactoryBean or @Configuration class actually works where you can make your own FilterConfig and push it in. Just so long as the web.xml doesn't also have values. If you want to make your suggestion a formal answer I would accept it as the answer. – ryber Sep 17 '15 at 21:21
  • Well I once prototyped a `BeanPostProcessor` to configure filters using the `Environment` abstraction in Spring. It can be found [here](https://github.com/mdeinum/spring-utils/blob/master/web/src/main/java/biz/deinum/beans/factory/annotation/FilterInitDestroyBeanPostProcessor.java) you basically define the properties in a properties file, load it using `@PropertySource` register this post processor, add the filter as a `@Bean` and it should get configured. – M. Deinum Sep 18 '15 at 06:07

0 Answers0