2V0-72.22PSE · Question #83
Spring bean classes are defined under a package called com.mycomp.service. What two options are required to enable component scanning? (Choose two.)
The correct answer is A. Ensure bean classes are annotated with the @Component annotation. B. Add the @ComponentScan ("com.mycomp. service") to the Java configuration. For Spring to automatically discover and register beans via component scanning, two things must work together: the bean classes must be marked with @Component (or a stereotype like @Service, @Repository) so Spring recognizes them as candidates (A), and the configuration class…
Question
Spring bean classes are defined under a package called com.mycomp.service. What two options are required to enable component scanning? (Choose two.)
Options
- AEnsure bean classes are annotated with the @Component annotation.
- BAdd the @ComponentScan ("com.mycomp. service") to the Java configuration.
- CEnsure bean classes are annotated with the @Autowired annotation.
- DEnsure bean classes are annotated with the @Bean annotation.
- EEnsure bean classes are annotated with the @ComponentScan annotation.
How the community answered
(44 responses)- A89% (39)
- C2% (1)
- D7% (3)
- E2% (1)
Explanation
For Spring to automatically discover and register beans via component scanning, two things must work together: the bean classes must be marked with @Component (or a stereotype like @Service, @Repository) so Spring recognizes them as candidates (A), and the configuration class must include @ComponentScan("com.mycomp.service") to tell Spring where to look (B) - without the package path, Spring won't know which packages to scan.
Why the distractors are wrong:
- C -
@Autowiredis for injecting dependencies into a bean, not for declaring it as a bean. - D -
@Beanis used on methods inside a@Configurationclass to manually define beans; it's not placed on bean classes themselves and has nothing to do with component scanning. - E -
@ComponentScanbelongs on the configuration class, not on individual bean classes; placing it on a bean class would have no effect.
Memory tip: Think of it as a two-sided contract - the beans raise their hand with @Component ("I'm here!"), and the config class uses @ComponentScan("package") to know which room to look in. Both sides must do their part.
Topics
Community Discussion
No community discussion yet for this question.