Bird
0
0

How can you define two beans of the same class but with different configurations in a single @Configuration class?

hard📝 Application Q8 of 15
Spring Boot - Spring Annotations
How can you define two beans of the same class but with different configurations in a single @Configuration class?
ADefine one @Bean method and annotate it twice with different names.
BUse the same @Bean method twice with different parameters.
CCreate one @Bean method and change its configuration at runtime.
DDefine two @Bean methods with different names and configure each bean separately.
Step-by-Step Solution
Solution:
  1. Step 1: Understand bean uniqueness

    Each @Bean method defines a unique bean identified by its method name.
  2. Step 2: Define multiple beans of the same type

    By creating two methods with different names and configurations, Spring registers two distinct beans.
  3. Final Answer:

    Define two @Bean methods with different names and configure each bean separately. -> Option D
  4. Quick Check:

    Use distinct @Bean methods with unique names for multiple beans [OK]
Quick Trick: Use distinct @Bean methods with unique names for multiple beans [OK]
Common Mistakes:
  • Trying to reuse the same @Bean method for multiple beans
  • Assuming one @Bean method can produce multiple differently configured beans
  • Misusing annotations to rename beans

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes