Sub topic 2
Constructor, Setter, and Field Injection
Assume you know Java classes, interfaces, constructors, annotations, and basic Maven or Gradle use. Spring dependency injection means Spring supplies an object’s dependencies instead of the object creating them.
Injection styles
Spring supports three common injection styles:
| Style | Main benefit | Common concern |
|---|---|---|
| Constructor | Required dependencies are explicit and easy to test | Constructors can become long |
| Setter | Supports optional or changeable dependencies | The object may be temporarily incomplete |
| Field | Very little code | Dependencies are hidden and testing is harder |
Prefer constructor injection for required dependencies. The class cannot be created without them, and its fields can be final. Setter injection fits optional dependencies. Field injection is usually avoided in application code because it hides the class’s requirements.
Defining beans with component scanning
A bean is an object managed by Spring. Component scanning searches selected packages for stereotype annotations and registers matching classes.
@Component is the general stereotype. @Service is a more descriptive stereotype for application or business services. Both can be injected.
public interface MessageSender {
void send(String to, String body);
}
@Service
public class NotificationService {
private final MessageSender sender;
public NotificationService(MessageSender sender) {
this.sender = sender;
}
public void welcome(String user) {
sender.send(user, "Welcome!");
}
}Your application must enable scanning, commonly through Spring Boot’s application class. Spring then creates NotificationService and supplies a matching MessageSender bean.
Defining beans with `@Configuration` and `@Bean`
Java configuration gives explicit control over object creation. A method annotated with @Bean registers its return value. Method parameters are resolved from other beans.
@Configuration
public class AppConfig {
@Bean
public MessageSender emailSender() {
return (to, body) -> System.out.println(to + ": " + body);
}
@Bean
public NotificationService notificationService(MessageSender sender) {
return new NotificationService(sender);
}
}Use component scanning for application classes you own. Use @Bean when construction needs library objects, conditional logic, or explicit configuration.
Resolving injection candidates
Spring first matches dependencies by type. If several beans have the same type, mark one default with @Primary, or select one explicitly with @Qualifier.
public AlertService(@Qualifier("smsSender") MessageSender sender) {
this.sender = sender;
}@Primary is useful for the normal choice. @Qualifier is clearer when a specific implementation matters.
Exercise
Which injection style should you normally use for a required dependency, and how do you select one bean when several share its type?
Show answer
Use constructor injection. Use @Primary for the default candidate or @Qualifier to select a named candidate.