Search code examples
spring-bootcontext-param

How to set context-param in spring-boot


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>p-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.


Solution

  • You can set parameters using the server.servlet.context-parameters application property. For example:

    server.servlet.context-parameters.p-name=p-value
    

    In Spring Boot 1.x, which is no longer supported, this property was named server.context-parameters:

    server.context-parameters=p-name=p-value
    

    Alternatively, you can configure parameters programmatically by declaring a ServletContextInitializer bean:

    @Bean
    public ServletContextInitializer initializer() {
        return servletContext -> {
            servletContext.setInitParameter("p-name", "p-value");
        };
    }