Show answerHide answer
Correct answer: B Ensure a valid @ComponentScan annotation in the Java configuration is specified.
Explanation
Explanation:Component Scanning (Option B): The @Component annotation is a stereotype marker indicating that a class is a candidate for auto-detection. However, Spring will not scan the classpath or instantiate the bean on its own. A configuration class must explicitly declare the @ComponentScan annotation (or use @SpringBootApplication, which implicitly includes it) and specify the base packages to search. This instructs the IoC container to scan the defined packages, locate the @Component classes, and automatically register them as beans.
Why the Other Options Are Incorrect
A is incorrect: Specifying an explicit bean name inside @Component("myBean") is optional. If left blank, Spring's BeanNameGenerator automatically derives a valid default name using the uncapitalized non-qualified class name.
C is incorrect: Explicitly declaring a @Scope is unnecessary. If omitted, Spring automatically assigns the default singleton scope to the auto-detected bean.
D is incorrect: The @Bean annotation is used inside @Configuration classes for explicit, manual bean registration. Combining @Component on a class with a separate @Bean factory method for that same class is redundant and incorrect for auto-detection.
References
Spring Framework Core Documentation: Section 1.10: Classpath Scanning and Managed Components – States that classpath scanning requires @ComponentScan alongside stereotype annotations like @Component to discover and register beans.
Spring Framework Core Documentation: Section 1.10.3: Automatically Detecting Classes and Registering Bean Definitions – Details how @ComponentScan triggers the parsing of components within specified packages.