@ComponentScan & Stereotypes — Concept Check
Questions on @ComponentScan & Stereotypes. @ComponentScan instructs Spring to detect @Component, @Service, @Repository, and @Controller classes and register them as beans automatically.
Questions in this quiz
5 questions on @ComponentScan & Stereotypes — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A Spring Boot application is not picking up a certain @Service annotated class. What could be a potential reason?
- AThe class is not in a package scanned by @ComponentScan.
- BThe class is not in the same package as the main application class.
- CThe class is missing a @Repository annotation.
- DThe class is not marked with @Controller.
2.A developer uses @Service("legacyOrderService") for a bean. What is the purpose of specifying "legacyOrderService"?
- ATo ensure it is the primary bean for injection.
- BTo specify the package where the service is located.
- CTo override the default bean name with a custom name.
- DTo indicate that the service is deprecated.
3.If two beans have the same name in a Spring application, what happens by default?
- AThe last definition wins in @ComponentScan.
- BSpring uses the first bean defined.
- CSpring throws a conflict exception in all cases.
- DSpring ignores both beans.
4.What is the role of the @Repository annotation in Spring?
- ATo mark a class for transaction management.
- BTo indicate a class is part of the service layer.
- CTo specify a class is a REST controller.
- DTo translate JDBC/JPA exceptions to DataAccessException.
5.A developer uses @Primary on one of two @Service beans. What is the effect of this annotation?
- AIt makes the annotated bean the default for injection when multiple beans of the same type exist.
- BIt excludes the annotated bean from being scanned.
- CIt marks the bean as deprecated.
- DIt ensures the bean is always injected by name.