Skip to content

Instantly share code, notes, and snippets.

@vlytsus
Created March 9, 2020 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vlytsus/a9b87f6a3d7c0e52e5c78bf8a2411d35 to your computer and use it in GitHub Desktop.
Save vlytsus/a9b87f6a3d7c0e52e5c78bf8a2411d35 to your computer and use it in GitHub Desktop.
Spring named beans injection
@Component("email")
public class EmailStrategy implements NotificationStrategy { ... }
...
@Component("sms")
public class SmsStrategy implements NotificationStrategy { ... }
...
@Service
public class NotificationService() {
@Resource(name = "email")
NotificationStrategy notifyEmail;
@Resource(name = "sms")
NotificationStrategy notifySms;
public void sendSMS(message) {
notifySms.send(message);
}
public void sendEmail(message) {
notifyEmail.send(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment