Bird
0
0

Given the following code, what will be the output when retrieving the bean named "service" from the Spring context?

medium📝 component behavior Q13 of 15
Spring Boot - Spring Annotations
Given the following code, what will be the output when retrieving the bean named "service" from the Spring context?
@Configuration
public class AppConfig {
  @Bean
  public String service() {
    return "MyService";
  }
}
A"service"
B"MyService"
CAn exception is thrown
Dnull
Step-by-Step Solution
Solution:
  1. Step 1: Identify the bean method and its return value

    The method named service returns the string "MyService" and is annotated with @Bean.
  2. Step 2: Understand bean retrieval by name

    Spring registers this bean with the name "service" and returns the method's return value when requested.
  3. Final Answer:

    "MyService" -> Option B
  4. Quick Check:

    Bean name returns method's return value [OK]
Quick Trick: Bean name matches method name; returns method's return value [OK]
Common Mistakes:
  • Expecting bean name as output instead of return value
  • Thinking Spring returns null if no explicit name
  • Assuming exception without error in code

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes