Java – Set a context-param in Spring Boot

This post shows you how to set a context-param in Spring Boot application. As you know, we often do this in a java web application via a deployment descriptor file (web.xml), for example:

web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
    <context-param>
        <param-name>backgroundColor</param-name>
        <param-value>red</param-value>
    </context-param>
</web-app>

But Spring Boot, it no longer relays on the XML configuration. So here are the 3 ways to do this:

1. Via application.properties file

src/main/resources/application.properties
server.context_parameters.backgroundColor=red

This way is for Spring Boot >= 1.2. But this approach (server.context-parameters.*) only works for embedded servers. So we should consider using the below approaches instead.
 

2. Via ServletContextInitializer bean

SpringBootWebApplication.java
package com.bytenota.springboot;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringBootWebApplication {

   public static void main(String[] args) {
      SpringApplication.run(SpringBootWebApplication.class, args);
   }
   
   @Bean
   public ServletContextInitializer initializer() {
        return new ServletContextInitializer() {

            @Override
            public void onStartup(ServletContext servletContext) throws ServletException {
                servletContext.setInitParameter("backgroundColor", "red");
            }
        };
   }

}

 

3. Via InitParameterConfiguringServletContextInitializer bean

SpringBootWebApplication.java
package com.bytenota.springboot;

import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.InitParameterConfiguringServletContextInitializer;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class SpringBootWebApplication  {
	
   public static void main(String[] args) {
      SpringApplication.run(SpringBootWebApplication.class, args);
   }
   
   @Bean
   public InitParameterConfiguringServletContextInitializer initParamsInitializer() {
       Map<String, String> contextParams = new HashMap<>();
       contextParams.put("backgroundColor", "red");
       return new InitParameterConfiguringServletContextInitializer(contextParams);
   }

}

0 0 vote
Article Rating
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
lurios
lurios
2 years ago

it works, thanks bro

josh sara
josh sara
2 years ago

it saves my time, thanks.

Dhron
Dhron
2 years ago

Really Awesome! Thank you so much really helped!

3
0
Would love your thoughts, please comment.x
()
x