0
0
Spring Bootframework~10 mins

@Component annotation in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to mark the class as a Spring component.

Spring Boot
@[1]
public class MyService {
    // service methods
}
Drag options to blanks, or click blank then click option'
ARepository
BService
CComponent
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service or @Repository instead of @Component when a generic component is needed.
Forgetting the @Component annotation, so Spring does not detect the class.
2fill in blank
medium

Complete the code to inject the component into another class using Spring's annotation.

Spring Boot
public class MyController {
    @[1]
    private MyService myService;
}
Drag options to blanks, or click blank then click option'
AInject
BAutowired
CResource
DComponent
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Autowired for injection.
Forgetting to annotate the field or constructor for injection.
3fill in blank
hard

Fix the error in the code by completing the annotation to register the class as a Spring component.

Spring Boot
@[1]
public class DataRepository {
    public void save() {
        // save data
    }
}
Drag options to blanks, or click blank then click option'
ARepository
BComponent
CService
DController
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Repository for data access classes.
Not annotating the class, so Spring does not manage it.
4fill in blank
hard

Fill both blanks to create a Spring component and inject it into another class.

Spring Boot
@[1]
public class NotificationService {
    public void notifyUser() {
        // notify logic
    }
}

public class UserController {
    @[2]
    private NotificationService notificationService;
}
Drag options to blanks, or click blank then click option'
AComponent
BAutowired
CService
DInject
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up @Service and @Component annotations.
Forgetting to use @Autowired for injection.
5fill in blank
hard

Fill all three blanks to define a Spring component with a method and inject it properly.

Spring Boot
@[1]
public class EmailSender {
    public void sendEmail() {
        // send email logic
    }
}

public class NotificationController {
    @[2]
    private EmailSender [3];
}
Drag options to blanks, or click blank then click option'
AComponent
BAutowired
CemailSender
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service instead of @Component when a generic component is intended.
Not matching the variable name to the injected type.