Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set context-param in spring-boot

Tags:

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.

like image 469
dom farr Avatar asked Oct 29 '14 19:10

dom farr


1 Answers

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 
like image 169
Andy Wilkinson Avatar answered Sep 22 '22 20:09

Andy Wilkinson