If a class is annotated with @Component, what should be done to have Spring automatically detect the annotated class and load it as a bean? (Choose the best answer.)
If a class is annotated with @Component, what should be done to have Spring automatically detect the annotated class and load it as a bean? (Choose the best answer.)
To have Spring automatically detect a class annotated with @Component and load it as a bean, you need to ensure a valid @ComponentScan annotation in the Java configuration. The @ComponentScan annotation tells Spring where to look for classes annotated with @Component (as well as other stereotype annotations such as @Service, @Repository, and @Controller) within the specified packages. Without this, Spring will not scan the specified packages for annotated classes.
To autodetect these classes and register the corresponding beans, you need to add @ComponentScan to your @Configuration class https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core
Valid bean name is provided for you using the name of the class, @ComponentScan on a @Configuration marked class is needed.
B is the correct answer: https://www.baeldung.com/spring-component-annotation
B. Ensure a valid @ComponentScan annotation in the Java configuration is specified. This is the correct answer. The @ComponentScan annotation tells Spring where to look for classes annotated with @Component (and other stereotype annotations like @Service, @Repository, and @Controller). Without this, Spring will not know to scan the specified packages for annotated classes.
Test to be
In order for spring to find the beans, you need to add @ComponentScan