How to set context-param in spring-boot

dom farr picture dom farr · Oct 29, 2014 · Viewed 38.8k times · Source

In the classic web.xml type configuration you could configure context parameters like so

web.xml

...
<context-param>
  <param-name>p-name</param-name>
  <param-value>-value</param-value>
</context-param>
...

How is this achieved in spring-boot. I have a filter that requires parameters.

I'm using @EnableAutoConfiguration and have included <artifactId>spring-boot-starter-jetty</artifactId> in my pom.

Answer

Andy Wilkinson picture Andy Wilkinson · Oct 30, 2014

You can set parameters on the whole ServletContext by declaring a ServletContextInitializer bean:

@Bean
public ServletContextInitializer initializer() {
    return new ServletContextInitializer() {

        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            servletContext.setInitParameter("p-name", "-value");
        }
    };
}

Update: in Spring Boot 1.2 using a ServletContextInitializer is no longer necessary. You can now configure a parameter on the ServletContext in a single line in application.properties:

server.context_parameters.p-name=-value