Bird
0
0

A developer writes:

medium📝 Debug Q7 of 15
Spring Boot - Spring Annotations
A developer writes:
@Profile("prod")
@Bean
public DataSource dataSource() { return new DataSource(); }

But the bean is created even when the active profile is 'dev'. What is the likely mistake?
AThe @Profile annotation should be on the configuration class, not the method.
BThe @Profile annotation is ignored on @Bean methods.
CThe @Profile annotation is placed on the method, but the configuration class is not annotated with @Configuration.
DThe active profile is not set correctly.
Step-by-Step Solution
Solution:
  1. Step 1: Understand @Profile on @Bean methods

    @Profile works on @Bean methods only if the containing class is annotated with @Configuration.
  2. Step 2: Identify missing @Configuration

    If the class lacks @Configuration, @Profile on methods is ignored and beans are created regardless of profile.
  3. Final Answer:

    The @Profile annotation is placed on the method, but the configuration class is not annotated with @Configuration. -> Option C
  4. Quick Check:

    @Profile on @Bean requires @Configuration class [OK]
Quick Trick: Use @Configuration on class when @Profile on @Bean methods [OK]
Common Mistakes:
  • Missing @Configuration on config class
  • Thinking @Profile ignored on @Bean methods
  • Incorrect active profile setting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes