How to set context-param in spring-boot
Asked Answered
C

4

25

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.

Cardigan answered 29/10, 2014 at 19:57 Comment(1)
Check out docs for application.properties options. Many of them you can resolve in just one line :) docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/….Silvestro
L
50

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");
    };
}
Loverly answered 30/10, 2014 at 8:33 Comment(5)
Your answer came up in some rather extensive research that I am doing about how to define a filter list using Spring Boot. I have been documenting my research. Are you willing to comment on it? Here is the link: #36489753Nichrome
@Andy how to set context-param programmatically for servlet 2.5?Landeros
Spring Boot does not support Servlet 2.5.Loverly
It has changed to "server.servlet.context-parameters.p-name=-value"Generator
In newest spring boot server.context_parameters is not working anymore - now this parameter is: server.servlet.context-parametersSaw
S
7

You can actually achieve this using Java config. If you have filter that requires some parameters, just put them in your application.yml (or .properties), inject them using @Value in your config class and register them in FilterRegistrationBean.

For example:

@Value("${myFilterParam}")
private String myFilterParam;

@Bean(name="myFilter")
public FilterRegistrationBean myFilter() {
    FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new MyFilter());
    filterRegistrationBean.setInitParameters(Collections.singletonMap("p-name", "p-value"));
    return filterRegistrationBean;
}

Also JavaDoc for FilterRegistrationBean:

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/context/embedded/FilterRegistrationBean.html

Update

You can register parameters for servlet context in SpringBootServletInitializer#onStartup() method. Your Application class can extend the SpringBootServletInitializer and you can override the onStartup method and set the parameters there. Example:

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

}

Other alternative is to define ServletContextInitializer bean as suggested by Andy Wilkinson.

Styptic answered 30/10, 2014 at 6:12 Comment(1)
Apologies, I was meant to ask about context params, not init params. I've updated the question to reflect this.Cardigan
Y
5

Since Spring Boot 2.0.0 they updated the way to add context param:

server.servlet.context-parameters.yourProperty.

You can see more updates on this link

Yam answered 9/1, 2020 at 14:24 Comment(0)
G
2

Also you can define InitParameterConfiguringServletContextInitializer in your configuration. Example:

@Bean
public InitParameterConfiguringServletContextInitializer initParamsInitializer() {
    Map<String, String> contextParams = new HashMap<>();
    contextParams.put("p-name", "-value");
    return new InitParameterConfiguringServletContextInitializer(contextParams);
}
Glovsky answered 28/9, 2017 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.