-
Notifications
You must be signed in to change notification settings - Fork 191
Dependency Injection
Ramesh Fadatare edited this page Jan 26, 2023
·
1 revision
package com.spring.core.dependency;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class MessageSender {
private MessageService messageService;
private MessageService smsService;
public MessageSender(@Qualifier("emailService") MessageService messageService){
this.messageService = messageService;
System.out.println("constructor based dependency injection");
}
@Autowired
public MessageSender(@Qualifier("emailService") MessageService messageService,
MessageService smsService){
this.messageService = messageService;
this.smsService = smsService;
System.out.println("constructor based dependency injection");
}
public void sendMessage(String message){
this.messageService.sendMessage(message);
smsService.sendMessage(message);
}
}
package com.spring.core.dependency;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class MessageSender {
private MessageService messageService;
private MessageService smsService;
// public MessageSender(@Qualifier("emailService") MessageService messageService){
// this.messageService = messageService;
// System.out.println("constructor based dependency injection");
// }
//
// @Autowired
// public MessageSender(@Qualifier("emailService") MessageService messageService,
// MessageService smsService){
// this.messageService = messageService;
// this.smsService = smsService;
// System.out.println("constructor based dependency injection");
// }
@Autowired
public void setMessageService(@Qualifier("emailService") MessageService messageService) {
this.messageService = messageService;
System.out.println("setter based dependency injection");
}
@Autowired
public void setSmsService(MessageService smsService) {
this.smsService = smsService;
}
public void sendMessage(String message){
this.messageService.sendMessage(message);
this.smsService.sendMessage(message);
}
}
package com.spring.core.dependency;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class MessageSender {
private MessageService messageService;
// public MessageSender(@Qualifier("emailService") MessageService messageService){
// this.messageService = messageService;
// System.out.println("constructor based dependency injection");
// }
//
// @Autowired
// public MessageSender(@Qualifier("emailService") MessageService messageService,
// MessageService smsService){
// this.messageService = messageService;
// this.smsService = smsService;
// System.out.println("constructor based dependency injection");
// }
@Autowired
public void setMessageService(@Qualifier("emailService") MessageService messageService) {
this.messageService = messageService;
System.out.println("setter based dependency injection");
}
public void sendMessage(String message){
this.messageService.sendMessage(message);
}
}
package com.spring.core.dependency;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class MessageSender {
@Autowired
@Qualifier("emailService")
private MessageService messageService;
@Autowired
private MessageService smsService;
// public MessageSender(@Qualifier("emailService") MessageService messageService){
// this.messageService = messageService;
// System.out.println("constructor based dependency injection");
// }
//
// @Autowired
// public MessageSender(@Qualifier("emailService") MessageService messageService,
// MessageService smsService){
// this.messageService = messageService;
// this.smsService = smsService;
// System.out.println("constructor based dependency injection");
// }
// @Autowired
// public void setMessageService(@Qualifier("emailService") MessageService messageService) {
// this.messageService = messageService;
// System.out.println("setter based dependency injection");
// }
//
// @Autowired
// public void setSmsService(MessageService smsService) {
// this.smsService = smsService;
// }
public void sendMessage(String message){
this.messageService.sendMessage(message);
this.smsService.sendMessage(message);
}
}