Complete the code to declare a Spring component using IoC.
@[1] public class MyService { // service logic }
The @Service annotation marks the class as a Spring service component managed by IoC.
Complete the code to inject a dependency using IoC in Spring.
public class MyController { @[1] private MyService myService; }
The @Autowired annotation tells Spring to inject the dependency automatically.
Fix the error in the code to properly use IoC for dependency injection.
public class MyController { private MyService myService; public MyController([1] MyService myService) { this.myService = myService; } }
The constructor parameter needs the @Autowired annotation for Spring to inject the dependency via constructor.
Fill both blanks to create a Spring bean and inject it using IoC.
@[1] public class NotificationService { } public class UserController { @[2] private NotificationService notificationService; }
@Component marks the class as a Spring bean, and @Autowired injects it where needed.
Fill all three blanks to define a Spring service, inject it, and use it in a method.
@[1] public class EmailService { public void sendEmail() { System.out.println("Email sent"); } } public class NotificationController { @[2] private EmailService emailService; public void notifyUser() { emailService.[3](); } }
@Service marks the class as a service bean, @Autowired injects it, and sendEmail() calls the method.