Recall & Review
beginner
What is the purpose of the
@Profile annotation in Spring Boot?The
@Profile annotation is used to mark beans that should only be created when a specific environment or profile is active. It helps manage environment-specific configurations easily.Click to reveal answer
beginner
How do you activate a Spring profile when running your application?
You can activate a Spring profile by setting the
spring.profiles.active property, for example in application.properties or as a command-line argument: --spring.profiles.active=dev.Click to reveal answer
intermediate
Can a bean have multiple profiles using
@Profile? How?Yes, you can specify multiple profiles in
@Profile by listing them as an array, like @Profile({"dev", "test"}). The bean will be created if any one of these profiles is active.Click to reveal answer
beginner
What happens if no profile is active and a bean is annotated with
@Profile("dev")?The bean will NOT be created because its profile does not match the active profile. Only beans with no profile or matching profiles are created.
Click to reveal answer
beginner
How can
@Profile help in real-life application development?It allows you to define different beans or configurations for environments like development, testing, and production. For example, you can use a mock service in development and a real service in production without changing code.
Click to reveal answer
What does the
@Profile("prod") annotation do on a Spring bean?✗ Incorrect
The
@Profile("prod") annotation ensures the bean is created only when the 'prod' profile is active.How do you specify multiple profiles in
@Profile?✗ Incorrect
Multiple profiles are specified as an array:
@Profile({"dev", "test"}).If no profile is active, which beans are created?
✗ Incorrect
Only beans without any
@Profile annotation are created if no profile is active.How can you activate a profile when running a Spring Boot app from the command line?
✗ Incorrect
Use
--spring.profiles.active=dev to activate the 'dev' profile.Why use
@Profile instead of if-else checks in code?✗ Incorrect
@Profile helps keep environment-specific code clean and separate, avoiding cluttered if-else logic.Explain how
@Profile helps manage different environments in a Spring Boot application.Think about how you might use different settings or services in development versus production.
You got /4 concepts.
Describe the steps to create and activate a Spring bean only for the 'test' environment using
@Profile.Consider both the bean code and how to run the app with the profile active.
You got /3 concepts.