0
0
Spring Bootframework~20 mins

Field injection and why to avoid it in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Field Injection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why is field injection discouraged in Spring Boot?
Which of the following is the main reason developers are advised to avoid field injection in Spring Boot?
AField injection automatically validates dependencies at compile time, preventing runtime errors.
BField injection improves code readability and is recommended for all Spring Boot projects.
CField injection makes unit testing harder because dependencies are private and cannot be set easily.
DField injection allows dependencies to be changed dynamically at runtime without restarting the application.
Attempts:
2 left
💡 Hint
Think about how private fields affect testing and flexibility.
component_behavior
intermediate
2:00remaining
What happens when a Spring component uses field injection?
Consider a Spring component with a private field annotated with @Autowired for dependency injection. What is the behavior when the application context starts?
ASpring calls the constructor with the dependency as a parameter to inject it.
BSpring requires a setter method to inject the dependency into the private field.
CSpring throws a compile-time error because private fields cannot be injected.
DSpring injects the dependency directly into the private field using reflection after the object is created.
Attempts:
2 left
💡 Hint
Think about how Spring accesses private fields.
🔧 Debug
advanced
2:00remaining
Why does this Spring Boot test fail with field injection?
Given this Spring Boot test class using field injection, why does the test fail with a NullPointerException? public class UserServiceTest { @Autowired private UserRepository userRepository; @Test public void testFindUser() { User user = userRepository.findById(1L); assertNotNull(user); } }
AField injection requires the userRepository field to be public for injection to work.
BThe test class is missing @ExtendWith(SpringExtension.class) or @SpringBootTest, so Spring does not inject dependencies.
CThe UserRepository bean is not annotated with @Component, so Spring cannot inject it.
DThe test method must be static for Spring to inject dependencies.
Attempts:
2 left
💡 Hint
Check if Spring context is loaded for the test.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly uses constructor injection in Spring Boot?
Select the code snippet that correctly implements constructor injection for a service class in Spring Boot.
A
public class OrderService {
  private final PaymentService paymentService;

  @Autowired
  public OrderService(PaymentService paymentService) {
    this.paymentService = paymentService;
  }
}
B
public class OrderService {
  private PaymentService paymentService;

  public void setPaymentService(PaymentService paymentService) {
    this.paymentService = paymentService;
  }
}
C
public class OrderService {
  private PaymentService paymentService;

  public OrderService() {
    this.paymentService = new PaymentService();
  }
}
D
public class OrderService {
  @Autowired
  private PaymentService paymentService;
}
Attempts:
2 left
💡 Hint
Constructor injection requires a constructor with parameters and @Autowired.
state_output
expert
2:00remaining
What is the state of the dependency in this Spring Boot component using field injection?
Given this Spring Boot component: @Component public class NotificationService { @Autowired private EmailService emailService; public void sendNotification() { emailService.sendEmail("Hello"); } } What happens if you create NotificationService with new NotificationService() and call sendNotification()?
AA NullPointerException occurs because emailService is not injected when using new operator.
BThe emailService is injected automatically even when using new operator, so the email is sent.
CThe code compiles but sendNotification() does nothing because emailService is empty.
DSpring throws a runtime error when new NotificationService() is called.
Attempts:
2 left
💡 Hint
Think about how Spring manages injection and object creation.