0
0
Spring Bootframework~10 mins

Constructor injection (preferred) 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 declare a constructor for dependency injection in a Spring Boot component.

Spring Boot
public class UserService {
    private final UserRepository userRepository;

    public UserService([1] userRepository) {
        this.userRepository = userRepository;
    }
}
Drag options to blanks, or click blank then click option'
AUserRepository
Bvoid
Cint
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Using a primitive type like int instead of the dependency class.
Using void as a parameter type, which is invalid.
2fill in blank
medium

Complete the code to annotate the class as a Spring component for constructor injection.

Spring Boot
[1]
public class OrderService {
    private final PaymentService paymentService;

    public OrderService(PaymentService paymentService) {
        this.paymentService = paymentService;
    }
}
Drag options to blanks, or click blank then click option'
A@Autowired
B@ComponentScan
C@Repository
D@Service
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired on the class instead of on the constructor or fields.
Using @ComponentScan which is for configuration classes.
3fill in blank
hard

Fix the error in the constructor injection by completing the annotation for automatic wiring.

Spring Boot
public class NotificationService {
    private final EmailSender emailSender;

    [1]
    public NotificationService(EmailSender emailSender) {
        this.emailSender = emailSender;
    }
}
Drag options to blanks, or click blank then click option'
A@Service
B@Inject
C@Autowired
D@Component
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Service or @Component on the constructor instead of the class.
Using @Inject which is a Java EE annotation, not Spring-specific.
4fill in blank
hard

Fill both blanks to complete a Spring component with constructor injection and final field declaration.

Spring Boot
@Component
public class [1] {
    private final [2] repository;

    public [1]([2] repository) {
        this.repository = repository;
    }
}
Drag options to blanks, or click blank then click option'
AProductService
BUserRepository
COrderRepository
DCustomerService
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching class name and constructor parameter types.
Using service names as repository types.
5fill in blank
hard

Fill all three blanks to complete a Spring service class with constructor injection and proper annotations.

Spring Boot
[1]
public class [2] {
    private final [3] paymentGateway;

    public [2]([3] paymentGateway) {
        this.paymentGateway = paymentGateway;
    }
}
Drag options to blanks, or click blank then click option'
A@Service
BPaymentService
CPaymentGateway
D@Component
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Component instead of @Service for service classes.
Mismatching class and parameter names.