4

I have a web application using Spring 4 and servlet-api 3.1 in which I am using AbstractAnnotationConfigDispatcherServletInitializer to initialize the DispatcherServlet and ContextLoaderListener via annotated @Configuration java-config Spring app contexts. Environment-specific configuration of the application is achieved using a PropertySourcesPlaceholderConfigurer with @PropertySource annotations. This all works nicely. My question is:

I am now in the position of overriding Filter[] getServletFilters() to add filters to the servlet context. However, for the init-param values of some of these filters, I would really like to be able to access some of the aforementioned environment-specific configuration values. Unfortunately, because the environments stuff is post-processing in the application contexts, it appears that I am unable to simply re-use the rather neat Spring configuration injection here. If at all possible, I would like to avoid either hard-coding values that are elsewhere dynamically configured or having to effectively duplicate the environment config logic provided by Spring.

Has anyone come up with a neat way to inject configured values that are shared with an app context into a WebApplicationInitializer?

Christopher
  • 206
  • 2
  • 4
  • Are you able to use Spring MVC interceptors instead of Servlet Filters? If so, then you can use all the usual Spring stuff without having to resort to some exotic configuration – geoand Jul 22 '14 at 09:16
  • 2
    Instead of adding the concrete filters add `DelegatingFilterProxy` instances instead and configure the filters in the application context. – M. Deinum Jul 22 '14 at 09:23
  • Thanks -- using `DelegatingFilterProxy` should work well. – Christopher Jul 22 '14 at 09:50
  • Just to note here for anyone who might be interested: a similar thing can be achieved for stubborn 3rd party servlets using `ServletWrappingController`. – Christopher Jul 22 '14 at 10:35

0 Answers0