Spring Profiles

Spring profile is a way to achieve environment specific execution of class or code i.e. Spring Profiles provide a way to segregate parts of your application configuration and make it be available only in certain environments.  If you want to run your piece of code or initialize any specific bean in any certain environment, Spring Profiles is the option.

Implementation

Any class marked with @Component or @Configuration, can also be marked @Profile in Spring application, as shown in the following example

@Configuration 
@Profile("production")
public class ProductionConfiguration {
// ...
}

As you can notice, the above bean is marked with production profile. i.e. if we mark our production profile active then only the ProductionConfiguration bean will be initialized otherwise it won’t.

Setting Profile

The above mentioned profile can be marked active with below configuration.

  • In application.properties or application.yml
spring.profiles.active=production
  • In command line by using the following switch
--spring.profiles.active=dev,hsqldb

  • Programmatically via ConfigurableEnvironment
@Autowired
private static ConfigurableEnvironment env;
public static void main(String[] args) {
    env.setActiveProfiles("production");
    SpringApplication.run(TektutorialSpringProfileApplication.class, args);
}
  • Programmatically via SpringApplication
    The SpringApplication entry point also has a Java API for setting additional profiles (that is, on top of those activated by the spring.profiles.active property).
public static void main(String[] args) {
         SpringApplication obj = new SpringApplication(TektutorialSpringProfileApplication.class);
         obj.setAdditionalProfiles("profile1", "profile2");
         obj.run(TektutorialSpringProfileApplication.class, args);
}
  • Maven profile
<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <spring.profiles.active>dev</spring.profiles.active>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <spring.profiles.active>production</spring.profiles.active>
        </properties>
    </profile>
</profiles>

Adding Active Profiles

We can specify default active profiles in application.properties and then replace them by using the command line switch. Command line inputs always override default properties mention in application.properteis.

The spring.profiles.include property can be used to unconditionally add active profiles in application.properties. Like below

spring.profiles: prod
spring.profiles.include:
- proddb
- prodmq

Lets Code

Lets create one simple spring boot application to show what we saw above.

I have created my application from
https://start.spring.io/ keeping only web as starter and the folder structure looks like below.

Overall folder structure of project
TektutorialSpringProfileApplication.java
BeanOne.java

BeanTwo.java
application.properties with profile UAT
Console output with Profile UAT
application.properties with profile DEV
Console output with Profile DEV

The above code can be found at the repo
https://github.com/tektutorial/tektutorial-spring-profile

Hope this must have cleared your doubts about Spring Profile. Thanks for reading.

Advertisements