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/


The first file 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 For example:

$ ./mvnw clean spring-boot:run

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 :: 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

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 where a H2 Datasource is configured:


An them, you could have an where an Oracle Datasource is configured:

spring.datasource.url= jdbc:oracle:thin:@//

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:

  profiles: dev
  port: 8000

  profiles: prod
  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

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:

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:

public class DemoMain {

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

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

public class SampleApplicationInitializer 
  implements WebApplicationInitializer {
    public void onStartup(ServletContext servletContext) throws ServletException {
          "", "prof");

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

private ConfigurableEnvironment env;


Declaring Profiles in XML

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

<beans profile="dev">
    <bean id="myBean"
      class="com.example.MyBean" />

Setting Profiles using environment variables

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

$ export spring_profiles_active=prod
FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials