Skip to content

Support configuring Servlet container in Spring using Servlet 3.0 programmatic config [SPR-7672] #12328

@spring-projects-issues

Description

@spring-projects-issues

Mike Youngstrom opened SPR-7672 and commented

This issue is a request to utilize Servlet 3.0 features to support the configuring of Servlets, Filters, Listeners, etc. using Spring configuration instead of forcing the user to configure that stuff in the web.xml. This type of configuration has a number of benefits. Some of those might be:

  • Direct support for dependency injected spring beans as Servlets, Filters, etc. Without the need to use indirection such as DelegatingFilterProxy and HttpRequestHandlerServlet
  • Support for configuration of servlet params (mappings, names, etc) with things such as SpringEL or PropertyPlaceholders
  • Support for modifying what servlets, filters, etc. may be configured in an application based on runtime environment (with runtime config scheduled for 3.1)
  • Potential simplification of Spring features/addons. For example auto configuration of SpringSecurity Filter as part of SpringSecurity namespace handlers, or SpringMVC servlet config, CharacterEncodingFilter, RequestLifecycleListener, etc.
  • Bootstrapping of the Sring ApplicationContext without having to specify the the SpringContextLoaderListener in the web.xml by simply using conventions to determine if spring should start itself up or not.
  • Potential for much much more.

I've attached a basic proof of concept example project. This example uses a ServletContainerInitializer to start a spring WebApplicationContext DURING ContainerStartup instead of after ServletContext Initialization. Not sure if this would break any existing functionality or not. My simple demo seems to work fine.

This ServletContainerInitializer looks for a file located in WEB-INF/applicationContext.xml or a contextConfigLocation init-param. If it finds either of these then it creates a WebApplicationContext uses ContextLoader.

This now enables a WebApplicationContext to add servlet configuration to the container at startup time. In my example I've created a simple bean that configures a DispatcherServlet in this web application and uses spring injected configuration to dictate how that Servlet gets configured.

This example is really ugly and more a proof of concept that you can use Spring to configure a servlet container as an alternative to web.xml. This example doesn't appear to me to eliminate any of the flexibility, customizability, extensibility, etc of Spring.

Some initial thoughts about work that might be involved in this issue:

  • Perhaps some Spring objects that combine both a filter or Servlet with configuration for that filter or servlet such as name, url mapping, etc.
  • These foundational objects could then potentially be automatically configured using a Spring PostProcessor or something.
  • Some namespace handlers to simplify adding and configuring a filter, servlet, etc in spring.
  • Perhaps some higher level configuration features to help simplify configuration of frameworks such as SpringMVC or Spring Security.

Thoughts?
Mike


Affects: 3.0.5

Attachments:

Issue Links:

3 votes, 7 watchers

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions