Spring Boot Profiles
In this tutorial, we will deep dive into the features that spring profile going to offer
Any @Component or @Configuration can be marked with @Profile. spring.profiles.active property is used to define your active profile. This can be mentioned either in the configuration file(application.properties or application.yml) or through program arguments. If an active profile is specified through program arguments it will replace the profile defined in configuration file. Sometimes it is useful to have profile-specific properties that add to the active profiles rather than replace them. The spring.profiles.include property can be used to unconditionally add active profiles.
You can also programmatically set your profiles using SpringApplication#setAdditionalProfiles().
Profile-specific variants of configurations can also be defined. For example for "development" profile application-development.properties file is read and for "production" profile application-production.properties file is read.
Spring Profiles:
Spring profiles help you to conditionally select various beans and configuration that should be included into the project. This will be very helpful if you work with multiple environments and configurations.Any @Component or @Configuration can be marked with @Profile. spring.profiles.active property is used to define your active profile. This can be mentioned either in the configuration file(application.properties or application.yml) or through program arguments. If an active profile is specified through program arguments it will replace the profile defined in configuration file. Sometimes it is useful to have profile-specific properties that add to the active profiles rather than replace them. The spring.profiles.include property can be used to unconditionally add active profiles.
You can also programmatically set your profiles using SpringApplication#setAdditionalProfiles().
Profile-specific variants of configurations can also be defined. For example for "development" profile application-development.properties file is read and for "production" profile application-production.properties file is read.
Find this project on Github
https://github.com/javacodenet/spring-boot-profiles/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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.javacodenet</groupId> | |
<artifactId>spring-boot</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>1.4.0.RELEASE</version> | |
</parent> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javacodenet; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RestController; | |
@RestController | |
@EnableAutoConfiguration | |
@SpringBootApplication | |
public class Application { | |
@Autowired | |
GenericConfiguration genericConfiguration; | |
@Value("${profileSpecificMessage}") | |
private String profileSpecificMessage; | |
@Value("${defaultProfileMessage}") | |
private String defaultProfileMessage; | |
@RequestMapping("/") | |
public String profiles() { | |
return genericConfiguration.configurationMessage() + ", default profile message: " + defaultProfileMessage + | |
", profile specific message: " + profileSpecificMessage; | |
} | |
public static void main(String[] args) { | |
SpringApplication springApplication = new SpringApplication(); | |
//this will add profile to existing set of profiles | |
springApplication.setAdditionalProfiles("default"); | |
SpringApplication.run(Application.class, args); | |
} | |
} |
GenericConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javacodenet; | |
public interface GenericConfiguration { | |
String configurationMessage(); | |
} |
ProdConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javacodenet; | |
import org.springframework.context.annotation.Profile; | |
import org.springframework.stereotype.Component; | |
@Profile({"production", "default"}) | |
@Component | |
public class ProdConfiguration implements GenericConfiguration { | |
@Override | |
public String configurationMessage() { | |
return "Production Configuration Message"; | |
} | |
} |
DevConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javacodenet; | |
import org.springframework.context.annotation.Profile; | |
import org.springframework.stereotype.Component; | |
@Profile({"development"}) | |
@Component | |
public class DevConfiguration implements GenericConfiguration { | |
@Override | |
public String configurationMessage() { | |
return "Development Configuration Message"; | |
} | |
} |
application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server.context-path=/profiles | |
spring.profiles.active=production | |
defaultProfileMessage = "default" |
application-production.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
profileSpecificMessage = "prod" |
application-development.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
profileSpecificMessage = "dev" |
Comments
Post a Comment