Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to inject the service using @Autowired.
Spring Boot
public class UserController { @[1] private UserService userService; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Inject instead of @Autowired in Spring Boot without proper configuration.
Forgetting the @Autowired annotation and manually instantiating the service.
✗ Incorrect
The @Autowired annotation tells Spring to inject the UserService dependency automatically.
2fill in blank
mediumComplete the code to inject the repository via constructor with @Autowired.
Spring Boot
public class ProductService { private final ProductRepository productRepository; @[1] public ProductService(ProductRepository productRepository) { this.productRepository = productRepository; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not annotating the constructor and expecting Spring to inject dependencies automatically.
Using @Inject without proper configuration in Spring Boot.
✗ Incorrect
Using @Autowired on the constructor tells Spring to inject the ProductRepository when creating ProductService.
3fill in blank
hardFix the error in the code by completing the annotation to inject the service.
Spring Boot
public class OrderController { @[1] private OrderService orderService; public void placeOrder() { orderService.processOrder(); } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service or @Component instead of @Autowired for injection.
Forgetting to annotate the field and causing NullPointerException.
✗ Incorrect
The @Autowired annotation is required to inject the OrderService dependency properly.
4fill in blank
hardFill both blanks to inject the repository and annotate the class as a Spring component.
Spring Boot
@[1] public class CustomerRepository { @[2] private DataSource dataSource; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Repository for repository classes.
Forgetting @Autowired on the dataSource field.
✗ Incorrect
The class should be annotated with @Repository to mark it as a Spring repository, and the dataSource field should be injected with @Autowired.
5fill in blank
hardFill all three blanks to create a service class with constructor injection and proper annotations.
Spring Boot
@[1] public class PaymentService { private final PaymentRepository paymentRepository; @[2] public PaymentService([3] paymentRepository) { this.paymentRepository = paymentRepository; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Service for service classes.
Forgetting @Autowired on the constructor.
Incorrect parameter type in the constructor.
✗ Incorrect
The class is annotated with @Service, the constructor with @Autowired, and the parameter type is PaymentRepository for proper dependency injection.