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

Merged
merged 6 commits into from
May 26, 2025
Merged
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
@@ -1,9 +1,9 @@

package com.baeldung.examples;
package com.baeldung;

import com.baeldung.examples.guice.Communication;
import com.baeldung.examples.guice.binding.AOPModule;
import com.baeldung.examples.guice.modules.BasicModule;
import com.baeldung.guice.Communication;
import com.baeldung.guice.binding.AOPModule;
import com.baeldung.guice.modules.BasicModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import java.util.Scanner;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

package com.baeldung.examples.guice;
package com.baeldung.guice;

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

import java.util.Date;
import java.util.LinkedList;
import java.util.Queue;
import java.util.logging.Logger;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

package com.baeldung.examples.guice;
package com.baeldung.guice;

import com.baeldung.examples.guice.constant.CommunicationModel;
import com.baeldung.guice.constant.CommunicationModel;

public interface CommunicationMode {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

package com.baeldung.examples.guice;
package com.baeldung.guice;

import com.baeldung.examples.guice.marker.Communicator;
import com.baeldung.guice.marker.Communicator;
import com.google.inject.Inject;
import com.google.inject.name.Named;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

package com.baeldung.examples.guice;
package com.baeldung.guice;

import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.baeldung.examples.guice.constant.CommunicationModel;
import com.baeldung.guice.aop.MessageSentLoggable;
import com.baeldung.guice.constant.CommunicationModel;

/**
*
Expand Down
4 changes: 4 additions & 0 deletions di-modules/guice/src/main/java/com/baeldung/guice/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.baeldung.guice;

public class Foo {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.examples.guice;
package com.baeldung.guice;

import com.google.inject.Inject;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.baeldung.examples.guice;
package com.baeldung.guice;

import com.baeldung.examples.common.PersonDao;
import com.baeldung.guice.common.PersonDao;
import com.google.inject.Inject;

public class GuicePersonService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.baeldung.examples.guice;
package com.baeldung.guice;

import com.baeldung.examples.common.AccountService;
import com.baeldung.guice.common.AccountService;
import com.google.inject.Inject;

public class GuiceUserService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

package com.baeldung.examples.guice;
package com.baeldung.guice;

import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.baeldung.examples.guice.constant.CommunicationModel;
import com.baeldung.guice.aop.MessageSentLoggable;
import com.baeldung.guice.constant.CommunicationModel;
import com.google.inject.Inject;
import java.util.logging.Logger;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.examples.guice;
package com.baeldung.guice;

public class Person {
private String firstName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

package com.baeldung.examples.guice;
package com.baeldung.guice;

import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.baeldung.examples.guice.constant.CommunicationModel;
import com.baeldung.guice.aop.MessageSentLoggable;
import com.baeldung.guice.constant.CommunicationModel;
import com.google.inject.Inject;
import java.util.logging.Logger;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

package com.baeldung.examples.guice.aop;
package com.baeldung.guice.aop;

import com.google.inject.Inject;
import java.util.logging.Logger;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

package com.baeldung.examples.guice.aop;
package com.baeldung.guice.aop;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

package com.baeldung.examples.guice.binding;
package com.baeldung.guice.binding;

import com.baeldung.examples.guice.aop.MessageLogger;
import com.baeldung.examples.guice.aop.MessageSentLoggable;
import com.baeldung.guice.aop.MessageLogger;
import com.baeldung.guice.aop.MessageSentLoggable;
import com.google.inject.AbstractModule;
import com.google.inject.matcher.Matchers;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

package com.baeldung.examples.guice.binding;
package com.baeldung.guice.binding;

import com.baeldung.examples.guice.Communication;
import com.baeldung.examples.guice.CommunicationMode;
import com.baeldung.examples.guice.DefaultCommunicator;
import com.baeldung.examples.guice.EmailCommunicationMode;
import com.baeldung.examples.guice.IMCommunicationMode;
import com.baeldung.examples.guice.SMSCommunicationMode;
import com.baeldung.guice.Communication;
import com.baeldung.guice.CommunicationMode;
import com.baeldung.guice.DefaultCommunicator;
import com.baeldung.guice.EmailCommunicationMode;
import com.baeldung.guice.IMCommunicationMode;
import com.baeldung.guice.SMSCommunicationMode;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
import java.util.logging.Level;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.examples.common;
package com.baeldung.guice.common;

public class Account {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.guice.common;

public interface AccountService {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.examples.common;
package com.baeldung.guice.common;

public class AccountServiceImpl implements AccountService {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.examples.common;
package com.baeldung.guice.common;

public interface AudioBookService {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.examples.common;
package com.baeldung.guice.common;

public class AudioBookServiceImpl implements AudioBookService {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.guice.common;

public interface AuthorService {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.examples.common;
package com.baeldung.guice.common;

public class AuthorServiceImpl implements AuthorService {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.guice.common;

public interface BookService {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.examples.common;
package com.baeldung.guice.common;

public class BookServiceImpl implements BookService {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.guice.common;

public interface PersonDao {

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.examples.common;
package com.baeldung.guice.common;

public class PersonDaoImpl implements PersonDao {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

package com.baeldung.examples.guice.constant;
package com.baeldung.guice.constant;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.baeldung.examples.guice.marker;
package com.baeldung.guice.marker;

/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

package com.baeldung.examples.guice.modules;
package com.baeldung.guice.modules;

import com.baeldung.examples.guice.Communication;
import com.baeldung.examples.guice.CommunicationMode;
import com.baeldung.examples.guice.DefaultCommunicator;
import com.baeldung.examples.guice.EmailCommunicationMode;
import com.baeldung.examples.guice.IMCommunicationMode;
import com.baeldung.examples.guice.SMSCommunicationMode;
import com.baeldung.guice.Communication;
import com.baeldung.guice.CommunicationMode;
import com.baeldung.guice.DefaultCommunicator;
import com.baeldung.guice.EmailCommunicationMode;
import com.baeldung.guice.IMCommunicationMode;
import com.baeldung.guice.SMSCommunicationMode;
import com.google.inject.AbstractModule;
import com.google.inject.name.Names;
import java.util.logging.Level;
Expand All @@ -24,9 +24,9 @@ protected void configure() {
bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.class));
bind(Boolean.class).toInstance(true);
} catch (NoSuchMethodException ex) {
Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex);
Logger.getLogger(com.baeldung.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex);
Logger.getLogger(com.baeldung.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex);
}
bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.baeldung.examples.guice.modules;
package com.baeldung.guice.modules;

import com.baeldung.examples.common.AccountService;
import com.baeldung.examples.common.AccountServiceImpl;
import com.baeldung.examples.common.BookService;
import com.baeldung.examples.common.BookServiceImpl;
import com.baeldung.examples.common.PersonDao;
import com.baeldung.examples.common.PersonDaoImpl;
import com.baeldung.examples.guice.Foo;
import com.baeldung.examples.guice.Person;
import com.baeldung.guice.common.AccountService;
import com.baeldung.guice.common.AccountServiceImpl;
import com.baeldung.guice.common.BookService;
import com.baeldung.guice.common.BookServiceImpl;
import com.baeldung.guice.common.PersonDao;
import com.baeldung.guice.common.PersonDaoImpl;
import com.baeldung.guice.Foo;
import com.baeldung.guice.Person;
import com.google.inject.AbstractModule;
import com.google.inject.Provider;
import com.google.inject.Provides;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.baeldung.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.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.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;
}
};
}
}
Loading