Sunday 29 July 2018

Spring Boot Profiles

Configuring application in an easy way through Spring Boot Profiles


Spring provides @Profile annotation using which we can deal with multiple profiles based of application environment. @Profile is used with @Configuration and spring stereotypes such as @Component, @Service etc. Different profile is created for different environment. We can also create a default profile that will work when there is no active profile.

To add active profile in property file, we need to configure spring.profiles.active=?. This will be included for every active profile. When we add active profile using command line then the active profile added in property file is replaced.

@ActiveProfiles: Spring test framework provides this annotation to use active profile in our test cases.

Properties file can be injected as external configuration to determine the behavior of the application at run time, to enable or disable the features and to provide extra required resources as per the different environment.

We have multiple environment in Application Life Cycle. Few examples are below:

a) development
b) qa
c) production

Spring Boot provides features through which we can specify different environment properties and we can set the active profile for the intended environment.

We can also pass the active profiles through command line which will not needed any change in application source.

$ mvn package

#Dev profile, application-development.properties

java -jar target/spring-boot-profiles-1.0.jar

{"Message":"A User Defined Message from DEVELOPMENT","Version":1.5,"Enable":false}

# set a profile dynamically through command line.

Note:- When we add any active profile in property file then application will start with that active profile and when we will add active profile through command line then in this case active profile configured in property file will be replaced by active profile passed in command line.

java -jar -Dspring.profiles.active=production target/spring-boot-profiles-1.0.jar
or

java -jar -Dspring.profiles.active="prod, test_prod" spring-boot-profiles-1.0.jar -> For multiple profiles

{"Message":"A User Defined Message from PRODUCTION","Version":1.1,"Enable":false}

Set Active Profiles Programmatically like below 

@SpringBootApplication
public class SpringBootProfilesApplication {
    public static void main(String[] args) {
SpringApplication application = new SpringApplication(SpringBootProfilesApplication.class);
application.setAdditionalProfiles("dev","test_dev");
application.run(args);
    }

Another way to set active profiles.

SpringApplication application = new SpringApplication(SpringBootProfilesApplication.class);
ConfigurableEnvironment environment = new StandardEnvironment();
environment.setActiveProfiles("dev","qa");
application.setEnvironment(environment);
application.run(args);

Example to set active profiles using Spring Boot.

Open https://start.spring.io/ and create an application with below details

         <groupId>com.gaurav</groupId>
<artifactId>spring-boot-profiles</artifactId>

Select web and devtools dependency and click on Generate Project.



1. pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.gaurav</groupId>
<artifactId>spring-boot-profiles</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>spring-boot-profiles</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

</project>

2. CustomConfiguration.java

package com.gaurav.springbootprofiles;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties("custom")
public class CustomConfiguration {

private boolean enable;
private String propMessage;
private double version;

public boolean isEnable() {
return enable;
}

public void setEnable(boolean enable) {
this.enable = enable;
}

public String getPropMessage() {
return propMessage;
}

public void setPropMessage(String propMessage) {
this.propMessage = propMessage;
}

public double getVersion() {
return version;
}

public void setVersion(double version) {
this.version = version;
}
}

3. SpringRestController.java

package com.gaurav.springbootprofiles;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SpringRestController {
@Autowired
private CustomConfiguration customConfiguration;
@Value("${greeting.propMessage}")
private String greetMessage;

@GetMapping("/greeting")
public String greetingMessage(String name) {
return "Hey "+ name+", " +greetMessage;
}

@GetMapping("/customConfiguration")
public Map<String, Object> dynamicConfiguration() {
Map<String, Object> map = new HashMap<>();
map.put("Message", customConfiguration.getPropMessage());
map.put("Version", customConfiguration.getVersion());
map.put("Enable", customConfiguration.isEnable());
return map;
}
}

4. SpringBootProfilesApplication.java

package com.gaurav.springbootprofiles;

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


@SpringBootApplication
public class SpringBootProfilesApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootProfilesApplication.class, args);
}
@Profile("production")
@Bean
public String productionBean() {
return "production";
}
@Profile("qa")
@Bean
public String qaBean() {
return "qa";
}
@Profile("development")
@Bean
public String developmentBean() {
return "development";
}
}

5. application.properties

server.port=9595
spring.profiles.active=production
app.name=spring-profiles-test

custom.enable: true
custom.propMessage: User Defined Message
custom.version: 2.5

logging.level.org.springframework.web.servlet: DEBUG
greeting.propMessage=A message from property file! Welcome to ${app.name}

6. application-development.properties

greeting.propMessage=A message from property file! Welcome to ${app.name} in DEVELOPMENT
custom.enable: false
custom.propMessage:A User Defined Message from DEVELOPMENT
custom.version: 1.5

7. application-production.properties

logging.level.org.springframework: INFO
greeting.propMessage=A message from property file! Welcome to ${app.name} in PRODUCTION
custom.enable: false
custom.propMessage:A User Defined Message from PRODUCTION
custom.version: 1.1

8. application-qa.properties

logging.level.org.springframework: INFO
greeting.propMessage=A message from property file! Welcome to ${app.name} in QA
custom.enable: false
custom.propMessage:A User Defined Message from QA Environment
custom.version: 1.1

Result:

Use URL:-

http://localhost:9595/greeting?name=gaurav


http://localhost:9595/customConfiguration





No comments:

Post a Comment