Following example shows how to set Environment active profile in Spring MVC.
In servlet based application, we need to add following in web.xml:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>profile1</param-value>
</context-param>
Example
Environment related beans
package com.logicbig.example;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
public interface GreetingService {
String getGreetingMsg();
@Service
@Profile("default")
class LocalGreetingService implements GreetingService {
@Override
public String getGreetingMsg() {
return "hi from local environment";
}
}
@Service
@Profile("dev")
class DevGreetingService implements GreetingService {
@Override
public String getGreetingMsg() {
return "hi from dev";
}
}
@Service
@Profile("prod")
class ProductionGreetingService implements GreetingService {
@Override
public String getGreetingMsg() {
return "hi from production";
}
}
}
Example controller
package com.logicbig.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ExampleController {
@Autowired
private GreetingService greetingService;
@GetMapping("/")
@ResponseBody
public String handle() {
return greetingService.getGreetingMsg();
}
}
Java Config
package com.logicbig.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
@ComponentScan
public class WebConfig extends WebMvcConfigurationSupport {
}
Web initializer
package com.logicbig.example;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
src/main/webapp/WEB-INF/web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>prod</param-value>
</context-param>
</web-app>
Running
To try examples, run embedded Jetty (configured in pom.xml of example project below):
mvn jetty:run
Output
$ curl -s "http://localhost:8080/" hi from production
Example ProjectDependencies and Technologies Used: - spring-webmvc 7.0.6 (Spring Web MVC)
Version Compatibility: 3.2.9.RELEASE - 7.0.6 Version compatibilities of spring-webmvc with this example: Versions in green have been tested.
- jakarta.servlet-api 6.1.0 (Jakarta Servlet API documentation)
- JDK 25
- Maven 3.9.11
|