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.
@Profile for environment-specific beans in 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.
@Profile("dev") @Component public class DevDataSource { // dev database config }
@Profile("prod") @Component public class ProdDataSource { // production database config }
@Profile({"dev", "test"})
@Component
public class DevOrTestService {
// bean active in dev or test profiles
}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.
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()); } }
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"}).
@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.
Practice
@Profile annotation in Spring Boot?Solution
Step 1: Understand the role of
The@Profile@Profileannotation is used to mark beans that should only be created when a specific environment profile is active.Step 2: Compare with other options
Options A, C, and D relate to other Spring Boot features but not to environment-specific bean creation.Final Answer:
To create beans only for specific environments -> Option CQuick Check:
@Profile controls bean creation per environment [OK]
- Confusing @Profile with @Component or @Service
- Thinking @Profile sets database URLs
- Assuming @Profile schedules tasks
@Profile on a Spring bean class for the "dev" environment?Solution
Step 1: Check correct syntax for @Profile
The@Profileannotation requires a string value in quotes, e.g.,@Profile("dev").Step 2: Analyze each option
@Profile(["dev"]) public class DevService {} uses square brackets, invalid Java annotation syntax. @Profile(dev) public class DevService {} misses quotes around "dev", syntax error. @Profile({dev}) public class DevService {} uses braces but misses quotes around "dev", syntax error. @Profile("dev") public class DevService {} uses correct quoted string syntax.Final Answer:
@Profile("dev") public class DevService {} -> Option AQuick Check:
@Profile needs quoted string [OK]
- Omitting quotes around profile names
- Using square brackets instead of curly braces
- Confusing array syntax with string
@Profile("dev")
@Component
public class DevBean {
public String getMessage() { return "Development Bean"; }
}
@Profile("prod")
@Component
public class ProdBean {
public String getMessage() { return "Production Bean"; }
}
@Component
public class BeanUser {
private final Object bean;
public BeanUser(@Autowired(required = false) Object bean) { this.bean = bean; }
public String show() { return bean == null ? "No Bean" : ((ProdBean)bean).getMessage(); }
}Solution
Step 1: Identify active profile and beans created
With profile "prod" active, onlyProdBeanis created.DevBeanis not created.Step 2: Analyze BeanUser constructor injection
BeanUser tries to autowire anObjectbean with@Autowired(required = false). Spring injects the availableProdBeaninstance as it matches.Step 3: Check the show() method
Inshow(),beanis cast toProdBean, which succeeds, returning "Production Bean".Final Answer:
Production Bean -> Option BQuick Check:
Prod profile loads ProdBean successfully [OK]
- Thinking Object type causes injection failure
- Expecting ClassCastException with matching profile
- Confusing which beans load per profile
@Profile("test") and @Profile("prod") respectively. When running the app with no active profile set, you get a NoSuchBeanDefinitionException. What is the most likely cause?Solution
Step 1: Understand @Profile behavior with no active profile
If no profile is active, Spring does not create beans annotated with any specific profile.Step 2: Analyze exception cause
NoSuchBeanDefinitionException means Spring could not find a bean to inject because none were created due to missing active profile.Final Answer:
No profile is active, so no beans with @Profile are created -> Option DQuick Check:
No active profile means no profile-specific beans [OK]
- Assuming beans load without active profile
- Blaming syntax errors without checking profiles
- Ignoring need for @Component on beans
@Profile annotation usage correctly achieves this?Solution
Step 1: Understand @Profile multiple values usage
Using@Profile({"dev", "test"})means the bean is active if either "dev" OR "test" profile is active.Step 2: Analyze other options
@Profile("dev & test") and D use string with "&" which is invalid syntax for multiple profiles. @Profile("dev | test") uses "|" inside string which is not supported syntax.Final Answer:
@Profile({"dev", "test"}) -> Option AQuick Check:
Use array of profiles for OR condition in @Profile [OK]
- Using string with & or | inside @Profile
- Confusing AND and OR logic in profiles
- Forgetting to use curly braces for multiple profiles
Ready to prove it?
Pass the test and this pattern moves unknown
