0
0
SpringbootConceptBeginner · 3 min read

@Profile Annotation in Spring: What It Is and How to Use It

The @Profile annotation in Spring lets you define beans or configurations that only load when a specific environment profile is active. It helps you separate settings for development, testing, or production by activating different profiles at runtime.
⚙️

How It Works

The @Profile annotation works like a filter for Spring beans or configuration classes. Imagine you have different outfits for summer and winter. You only wear the summer outfit when it's warm and the winter outfit when it's cold. Similarly, Spring only loads beans marked with a certain profile when that profile is active.

When you start your Spring application, you can specify which profile is active, such as dev or prod. Spring then scans for beans or configurations tagged with @Profile matching the active profile and loads only those. This way, you can have different database settings, service implementations, or feature toggles for each environment without changing your code.

💻

Example

This example shows two service beans with different profiles: one for development and one for production. Spring will load only the bean matching the active profile.

java
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;

public interface GreetingService {
    String greet();
}

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

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

// In application.properties or as a command line argument:
// spring.profiles.active=dev

// When running with 'dev' profile active, calling GreetingService.greet() returns:
// "Hello from Development!"

// When running with 'prod' profile active, it returns:
// "Hello from Production!"
Output
Hello from Development! (if 'dev' profile active) Hello from Production! (if 'prod' profile active)
🎯

When to Use

Use @Profile when you want to run different code or configurations depending on the environment. For example:

  • Loading a mock service in development but a real service in production.
  • Using an in-memory database for testing and a real database for production.
  • Enabling debug logging only in development.

This helps keep your code clean and avoids manual changes when moving between environments.

Key Points

  • @Profile controls which beans or configs load based on active environment profiles.
  • Profiles are activated via properties or command line arguments.
  • It helps separate environment-specific logic cleanly.
  • Supports multiple profiles on one bean using arrays.

Key Takeaways

@Profile lets Spring load beans only for specific environments.
Activate profiles via spring.profiles.active property or command line.
Use it to separate dev, test, and prod configurations cleanly.
You can assign multiple profiles to a bean by listing them in @Profile.
It keeps your application flexible and easier to manage across environments.