0
0
Spring Bootframework~5 mins

@Profile for environment-specific beans in Spring Boot

Choose your learning style9 modes available
Introduction

The @Profile annotation helps you create different versions of a bean for different environments, like development or production. This way, your app uses the right settings automatically.

When you want to use a special database connection for testing but a different one for production.
When you need to enable debug logging only in development but not in production.
When you want to switch between different service implementations based on the environment.
When you want to avoid changing code manually for each environment.
When you want to keep environment-specific settings clean and organized.
Syntax
Spring Boot
@Profile("profileName")
@Component
public class YourBean {
    // bean code here
}

The @Profile annotation goes on a class or method that creates a bean.

The profileName is a string that matches the active environment profile.

Examples
This bean is only active when the 'dev' profile is active.
Spring Boot
@Profile("dev")
@Component
public class DevDataSource {
    // dev database config
}
This bean is only active when the 'prod' profile is active.
Spring Boot
@Profile("prod")
@Component
public class ProdDataSource {
    // production database config
}
This bean works in either 'dev' or 'test' environments.
Spring Boot
@Profile({"dev", "test"})
@Component
public class DevOrTestService {
    // bean active in dev or test profiles
}
Sample Program

This example shows two beans with @Profile: one for 'dev' and one for 'prod'. When you run the app with --spring.profiles.active=dev, it prints the development greeting. With prod active, it prints the production greeting.

Spring Boot
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

public interface GreetingService {
    String greet();
}

@Profile("dev")
@Component
public class DevGreetingService implements GreetingService {
    public String greet() {
        return "Hello from Development!";
    }
}

@Profile("prod")
@Component
public class ProdGreetingService implements GreetingService {
    public String greet() {
        return "Hello from Production!";
    }
}

// Usage example in a Spring Boot app
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProfileDemoApplication implements CommandLineRunner {

    @Autowired
    private GreetingService greetingService; // Dev or Prod based on active profile

    public static void main(String[] args) {
        SpringApplication.run(ProfileDemoApplication.class, args);
    }

    @Override
    public void run(String... args) {
        System.out.println(greetingService.greet());
    }
}
OutputSuccess
Important Notes

You activate a profile by setting spring.profiles.active in application.properties or as a command line argument.

If no profile matches, Spring won't create the bean with @Profile.

You can use multiple profiles by listing them in @Profile({"dev", "test"}).

Summary

@Profile lets you create beans only for certain environments.

Use it to keep your app flexible and environment-aware without changing code.

Activate profiles easily with Spring Boot settings or command line options.