0
0
Spring Bootframework~10 mins

@Autowired for dependency injection 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 inject the service using @Autowired.

Spring Boot
public class UserController {
    @[1]
    private UserService userService;
}
Drag options to blanks, or click blank then click option'
AAutowired
BInject
CService
DComponent
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.
2fill in blank
medium

Complete 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'
AAutowired
BInject
CService
DComponent
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.
3fill in blank
hard

Fix 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'
AComponent
BAutowired
CService
DRepository
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service or @Component instead of @Autowired for injection.
Forgetting to annotate the field and causing NullPointerException.
4fill in blank
hard

Fill 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'
AComponent
BAutowired
CRepository
DService
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Repository for repository classes.
Forgetting @Autowired on the dataSource field.
5fill in blank
hard

Fill 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'
AService
BAutowired
CPaymentRepository
DComponent
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.