Skip to content

[BAEL-6028] Guice Provider and @Provides #18511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.baeldung.examples.guice.provider;

import com.google.inject.Provider;
import java.util.logging.Logger;

public class EmailNotifier implements Notifier, Provider<Notifier> {

private String smtpUrl;
private String user;
private String password;
private EmailNotifier emailNotifier;
Logger log = Logger.getLogger(EmailNotifier.class.getName());

@Override
public Notifier get() {
// perform some initialization for email notifier
this.smtpUrl = "smtp://localhost:25";
emailNotifier = new EmailNotifier();
return emailNotifier;
}

@Override
public void sendNotification(String message) {
log.info("Sending email notification: " + message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.baeldung.examples.guice.provider;


public interface Logger {
String log(String message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baeldung.examples.guice.provider;

import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.name.Names;

public class MyGuiceModule extends AbstractModule {
/**
* This method is called when the Guice injector is created.
* It binds the Notifier interface to the EmailNotifier implementation.
*/

@Override
protected void configure() {
bind(Notifier.class).annotatedWith(Names.named("Email"))
.toProvider(EmailNotifier.class);

bind(Notifier.class).annotatedWith(Names.named("Phone"))
.toProvider(PhoneNotifier.class);
}

@Provides
public Logger provideLogger() {
return new Logger() {
@Override
public String log(String message) {
return "Logging message: " + message;
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.examples.guice.provider;

import com.google.inject.Inject;
import com.google.inject.name.Named;

public class MyService {
private final Notifier emailNotifier;
private final Notifier phoneNotifier;

@Inject
public MyService(@Named("Email") Notifier emailNotifier, @Named("Phone") Notifier phoneNotifier) {
this.emailNotifier = emailNotifier;
this.phoneNotifier = phoneNotifier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.examples.guice.provider;

public interface Notifier {
void sendNotification(String message);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.baeldung.examples.guice.provider;

import java.util.logging.Logger;

import com.google.inject.Provider;

public class PhoneNotifier implements Notifier, Provider<Notifier> {
private String fromNumber;
private String toNumber;

private PhoneNotifier phoneNotifier;
Logger log = Logger.getLogger(EmailNotifier.class.getName());

@Override
public Notifier get() {
// perform some initialization for email notifier
this.fromNumber = "baeldung";
phoneNotifier = new PhoneNotifier();
return phoneNotifier;
}

@Override
public void sendNotification(String message) {
log.info("Sending phone notification: " + message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.baeldung.java;

import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import com.baeldung.examples.guice.provider.MyGuiceModule;
import com.baeldung.examples.guice.provider.Notifier;
import com.baeldung.examples.guice.provider.EmailNotifier;
import com.baeldung.examples.guice.provider.Logger;

import com.baeldung.examples.guice.provider.PhoneNotifier;
import com.google.inject.Guice;
import com.google.inject.Injector;

public class GuiceProviderUnitTest {
@Test
public void givenGuiceProvider_whenInjecting_thenShouldReturnEmailNotifier() {
// Create a Guice injector with the NotifierModule
Injector injector = Guice.createInjector(new MyGuiceModule());
// Get an instance of Notifier from the injector
Notifier emailNotifier = injector.getInstance(EmailNotifier.class);
Notifier phoneNotifier = injector.getInstance(PhoneNotifier.class);
// Assert that notifier is of type EmailNotifier
assert emailNotifier != null;

assert emailNotifier instanceof EmailNotifier;
assert phoneNotifier instanceof PhoneNotifier;
}

@Test
public void givenGuiceProvider_whenInjectingWithProvides_thenShouldReturnCustomLogger() {
// Create a Guice injector with the NotifierModule
Injector injector = Guice.createInjector(new MyGuiceModule());
// Get an instance of Logger from the injector
Logger logger = injector.getInstance(Logger.class);
assert logger != null;
Assertions.assertNotNull(logger.log("Hello world"));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.examples;
package com.baeldung.java;

import static org.junit.Assert.assertNotNull;

Expand Down