Using Profiles with Spring Boot

This tutorial will provide a brief introduction to Spring Boot Profiles and how they can make your applications more flexible.

A Spring Boot profile gives you the ability to flex your configuration based on the environment where the code is deployed to. For example a devevelopment environment could require different application settings compared with a production or pre-production settings.

Let’s see a practical example. In the following project structure, we have defined the following property files:

$ tree src/main/resources/  src/main/resources/ ├── application-dev.properties ├── application-prod.properties 

The first file -application-dev.properties- contains a Web server port settings:

server.port = 8180 

On the other hand, the other file contains the following settings:

server.port = 8080 

The simplest way to choose on profile is by passing to the JVM the -Dspring.profiles.active=. For example:

$ ./mvnw clean spring-boot:run -Dspring.profiles.active=prod   .   ____          _            __ _ _  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )   '  |____| .__|_| |_|_| |_\__, | / / / /  =========|_|==============|___/=/_/_/_/  :: Spring Boot ::        (v2.1.1.RELEASE)  2018-12-04 17:27:50.362  INFO 9969 --- [           main] com.example.demomvc.DemoMvcApplication   : Starting DemoMvcApplication on localhost.localdomain with PID 9969 (/home/francesco/springboot/demo-mvc/target/classes started by francesco in /home/francesco/springboot/demo-mvc) 2018-12-04 17:27:50.367  INFO 9969 --- [           main] com.example.demomvc.DemoMvcApplication   : The following profiles are active: prod 

As you can see from the logs, the active profile is “prod”.

Much the same way you can run your example with the following:

$ ./mvnw clean spring-boot:run -Dspring.profiles.active=dev 

A more complex Spring Boot Profile

In real world scenarios, you would use Profiles to define your database settings which are usually different in development, production or test environment. So you could have an application-dev.properties where a H2 Datasource is configured:

spring.datasource.driver-class-name=org.h2.Driver spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 spring.datasource.username=sa spring.datasource.password=sa 

An them, you could have an application-production.properties where an Oracle Datasource is configured:

spring.datasource.url= jdbc:oracle:thin:@//192.168.10.1:1521/ORCL spring.datasource.username=system spring.datasource.password=manager spring.datasource.driver-class-name=oracle.jdbc.OracleDriver 

Declaring multiple profiles in a configuration file

It is also possible to declare multiple profiles in a single configuration file. For example, take the following application.yml file which contains definition of two profiles, each one with different properties:

spring:   profiles: dev server:   port: 8000

spring:   profiles: prod server:   port: 9000 

Much the same way, you could run one profile or another by passing as JVM start up argument:

$ ./mvnw clean spring-boot:run -Dspring.profiles.active=prod 

Use @Profile on a Bean

Profiles can also be declared in your Beans via the @Profile annotation. In this case, a Bean will be available as long as you are using the Profile that is specified in the annotation:

@Component @Profile("dev") public class MyBean 

If you are looking for a programmatic way to choose the Profile, then you can do that too! Consider the following application class:

@SpringBootApplication
public class DemoMain {
  public static void main(String[] args) {
    System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, "prod");
    SpringApplication sa = new SpringApplication(DemoMain.class);
    sa.run(args);
  }
}

Then, it’s worth to mention that Web start-up classes are able to set the active profile through the ServletContext:

@Configuration
public class SampleApplicationInitializer implements WebApplicationInitializer {
  @Override
  public void onStartup(ServletContext servletContext) throws ServletException {
    servletContext.setInitParameter("spring.profiles.active", "prof");
  }
}

Finally, You can also set profiles directly on the environment:

@Autowired private ConfigurableEnvironment env;
env.setActiveProfiles("prod");

Declaring Profiles in XML

Profiles can be configured as well in old XML configuration files:

<?xml version="1.0" encoding="UTF-8"?><beans profile="dev">
        
   <bean id="myBean" class="com.example.MyBean"/>
    
</beans>

Setting Profiles using environment variables

On a Linux environment, profiles can also be activated via the environment variable:

$ export spring_profiles_active=prod 
Found the article helpful? if so please follow us on Socials
Twitter Icon       Facebook Icon       LinkedIn Icon       Mastodon Icon