0
0
Spring Bootframework~20 mins

@Component, @Service, @Repository, @Controller in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spring Annotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the primary role of the @Service annotation in Spring Boot?
Choose the option that best describes what @Service does in a Spring Boot application.
AIndicates a class is responsible for handling HTTP requests and returning responses.
BMarks a class as a service provider to hold business logic and be managed by Spring's container.
CDesignates a class as a data access object that interacts with the database.
DMarks a class as a generic component without any specific role.
Attempts:
2 left
💡 Hint
Think about where you put your business rules and logic in a Spring app.
📝 Syntax
intermediate
2:00remaining
Which annotation should you use to mark a class that directly accesses the database?
Select the correct Spring annotation for a class that handles database operations.
A@Repository
B@Service
C@Controller
D@Component
Attempts:
2 left
💡 Hint
This annotation also helps with exception translation for database errors.
component_behavior
advanced
2:00remaining
What happens if you annotate a class with @Component but also annotate it with @Controller in Spring Boot?
Choose the correct statement about the behavior of a class annotated with both @Component and @Controller.
ASpring treats it as a controller, and @Component is redundant but harmless.
BSpring throws a runtime error due to conflicting annotations.
CSpring treats it only as a generic component and ignores @Controller.
DThe class will not be registered as a bean in the Spring container.
Attempts:
2 left
💡 Hint
Consider how Spring handles meta-annotations and specialization.
🔧 Debug
advanced
2:00remaining
Why does Spring fail to inject a bean annotated only with @Component in a package outside the main application package?
Given a Spring Boot app, a class annotated with @Component is in a package not scanned by Spring. What is the cause of the injection failure?
ASpring requires explicit XML configuration to inject @Component beans.
B@Component beans cannot be injected unless also annotated with @Service.
CSpring only scans packages under the main application class package by default.
DThe class must be annotated with @Repository to be injected.
Attempts:
2 left
💡 Hint
Think about how Spring Boot finds beans automatically.
lifecycle
expert
3:00remaining
In what order does Spring instantiate and inject beans annotated with @Component, @Service, @Repository, and @Controller during application startup?
Select the correct order of bean instantiation and injection by Spring Boot for these annotations.
A@Component beans first, then @Repository, then @Service, then @Controller
B@Repository beans first, then @Component, then @Service, then @Controller
C@Controller beans first, then @Service, then @Repository, then @Component
DSpring does not guarantee a specific order; it instantiates beans as needed based on dependencies.
Attempts:
2 left
💡 Hint
Think about how Spring resolves dependencies and manages bean lifecycle.