
- Guice - Home
- Guice - Overview
- Guice - Environment Setup
- Guice - First Application
- Bindings Examples
- Guice - Linked binding
- Guice - Binding Annotations
- Guice - @Named binding
- Guice - Constant Bindings
- Guice - @Provides Annotation
- Guice - Provider Class
- Guice - Constructor Bindings
- Guice - Inbuilt Bindings
- Guice - Just-in-time Bindings
- Injection Examples
- Guice - Constructor Injection
- Guice - Method Injection
- Guice - Field Injection
- Guice - Optional Injection
- Guice - On-demand Injection
- Miscellaneous Examples
- Guice - Scopes
- Guice - AOP
- Guice Useful Resources
- Guice - Quick Guide
- Guice - Useful Resources
- Guice - Discussion
Guice - Quick Guide
Guice - Overview
Guice is an open source, Java-based dependency injection framework. It is quiet lightweight and is actively developed/managed by Google.
Dependency Injection
Every Java-based application has a few objects that work together to present what the end-user sees as a working application. When writing a complex Java application, application classes should be as independent as possible of other Java classes to increase the possibility to reuse these classes and to test them independently of other classes while unit testing. Dependency Injection (or sometime called wiring) helps in gluing these classes together and at the same time keeping them independent.
Consider you have an application which has a text editor component and you want to provide a spell check. Your standard code would look something like this â
public class TextEditor { private SpellChecker spellChecker; public TextEditor() { spellChecker = new SpellChecker(); } }
What we've done here is, create a dependency between the TextEditor and the SpellChecker. In an inversion of control scenario, we would instead do something like this â
public class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor(SpellChecker spellChecker) { this.spellChecker = spellChecker; } }
Here, the TextEditor should not worry about SpellChecker implementation. The SpellChecker will be implemented independently and will be provided to the TextEditor at the time of TextEditor instantiation.
Dependency Injection using Guice (Binding)
Dependency Injection is controlled by the Guice Bindings. Guice uses bindings to map object types to their actual implementations. These bindings are defined a module. A module is a collection of bindings as shown below:
public class TextEditorModule extends AbstractModule { @Override protected void configure() { /* * Bind SpellChecker binding to WinWordSpellChecker implementation * whenever spellChecker dependency is used. */ bind(SpellChecker.class).to(WinWordSpellChecker.class); } }
The Module is the core building block for an Injector which is Guice's object-graph builder. First step is to create an injector and then we can use the injector to get the objects.
public static void main(String[] args) { /* * Guice.createInjector() takes Modules, and returns a new Injector * instance. This method is to be called once during application startup. */ Injector injector = Guice.createInjector(new TextEditorModule()); /* * Build object using injector */ TextEditor textEditor = injector.getInstance(TextEditor.class); }
In above example, TextEditor class object graph is constructed by Guice and this graph contains TextEditor object and its dependency as WinWordSpellChecker object.
Guice - Environment Setup
This chapter will guide you on how to prepare a development environment to start your work with Google GSON. It will also teach you how to set up JDK on your machine before you set up GSON −
Setup Java Development Kit (JDK)
You can download the latest version of SDK from Oracle's Java site − Java SE Downloads. You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively.
If you are running Windows and have installed the JDK in C:\jdk-24, you would have to put the following line in your C:\autoexec.bat file.
set PATH=C:\jdk-24;%PATH% set JAVA_HOME=C:\jdk-24
Alternatively, on Windows NT/2000/XP, you will have to right-click on My Computer, select Properties → Advanced → Environment Variables. Then, you will have to update the PATH value and click the OK button.
On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk-24 and you use the C shell, you will have to put the following into your .cshrc file.
setenv PATH /usr/local/jdk-24/bin:$PATH setenv JAVA_HOME /usr/local/jdk-24
Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, you will have to compile and run a simple program to confirm that the IDE knows where you have installed Java. Otherwise, you will have to carry out a proper setup as given in the document of the IDE.
Popular Java Editors
To write your Java programs, you need a text editor. There are many sophisticated IDEs available in the market. But for now, you can consider one of the following −
Notepad − On Windows machine, you can use any simple text editor like Notepad (Recommended for this tutorial), TextPad.
Netbeans − It is a Java IDE that is open-source and free, which can be downloaded from www.netbeans.org/index.html.
Eclipse − It is also a Java IDE developed by the eclipse open-source community and can be downloaded from www.eclipse.org.
Google Guice Environment
Download the latest version of Google Guice and related jar files.
At the time of writing this tutorial, we have copied them into C:\>Google folder.
OS | Archive name |
---|---|
Windows | guice-7.0.0.jar;aopalliance-1.0.jar;guava-33.4.8-jre;jakarta.inject-api-2.0.1;asm-9.8.jar |
Linux | guice-7.0.0.jar;aopalliance-1.0.jar;guava-33.4.8-jre;jakarta.inject-api-2.0.1;asm-9.8.jar |
Mac | guice-7.0.0.jar;aopalliance-1.0.jar;guava-33.4.8-jre;jakarta.inject-api-2.0.1;asm-9.8.jar |
Set CLASSPATH Variable
Set the CLASSPATH environment variable to point to the Guice jar location. Assuming, you have stored Guice and related jars in Google folder on various Operating Systems as follows.
OS | Output |
---|---|
Windows | Set the environment variable CLASSPATH to %CLASSPATH%;C:\Google\guice-7.0.0.jar;C:\Google\aopalliance-1.0.jar;C:\Google\asm-9.8.jar;C:\Google\guava-33.4.8-jre.jar;C:\Google\jakarta.inject-api-2.0.1;.; |
Linux | export CLASSPATH=$CLASSPATH:Google/guice-7.0.0.jar:Google/aopalliance-1.0.jar:C:\Google\asm-9.8.jar:Google/guava-33.4.8-jre.jar:Google/jakarta.inject-api-2.0.1:. |
Mac | export CLASSPATH=$CLASSPATH:Google/guice-7.0.0.jar:Google/aopalliance-1.0.jar:C:\Google\asm-9.8.jar:Google/guava-33.4.8-jre.jar:Google/jakarta.inject-api-2.0.1.jar:. |
Guice - First Application
Let's create a sample console based application where we'll demonstrate dependency injection using Guice binding mechanism step by step.
Step 1: Create Interface
//spell checker interface interface SpellChecker { public void checkSpelling(); }
Step 2: Create Implementation
//spell checker implementation class SpellCheckerImpl implements SpellChecker { @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); } }
Step 3: Create Bindings Module
//Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { bind(SpellChecker.class).to(SpellCheckerImpl.class); } }
Step 4: Create Class with dependency
class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor(SpellChecker spellChecker) { this.spellChecker = spellChecker; } public void makeSpellCheck(){ spellChecker.checkSpelling(); } }
Step 5: Create Injector
Injector injector = Guice.createInjector(new TextEditorModule());
Step 6: Get Object with dependency fulfilled.
TextEditor editor = injector.getInstance(TextEditor.class);
Step 7: Use the object.
editor.makeSpellCheck();
Example - Dependency Injection using Guice
GuiceTester.java
\ package com.tutorialspoint; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeSpellCheck(); } } class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor(SpellChecker spellChecker) { this.spellChecker = spellChecker; } public void makeSpellCheck(){ spellChecker.checkSpelling(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { bind(SpellChecker.class).to(SpellCheckerImpl.class); } } //spell checker interface interface SpellChecker { public void checkSpelling(); } //spell checker implementation class SpellCheckerImpl implements SpellChecker { @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); } }
Output
Compile and run the file, you will see the following output −
Inside checkSpelling.
Guice - Linked Binding
Overview
In Linked bindings, Guice maps a type to its implementation. In below example, we've mapped SpellChecker interface with its implementation SpellCheckerImpl.
bind(SpellChecker.class).to(SpellCheckerImpl.class);
We can also mapped the concrete class to its subclass. See the example below:
bind(SpellCheckerImpl.class).to(WinWordSpellCheckerImpl.class);
Here we've chained the bindings. Let's see the result in complete example.
Example - Usage of Linked Binding
GuiceTester.java
package com.tutorialspoint; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeSpellCheck(); } } class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor(SpellChecker spellChecker) { this.spellChecker = spellChecker; } public void makeSpellCheck(){ spellChecker.checkSpelling(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { bind(SpellChecker.class).to(SpellCheckerImpl.class); bind(SpellCheckerImpl.class).to(WinWordSpellCheckerImpl.class); } } //spell checker interface interface SpellChecker { public void checkSpelling(); } //spell checker implementation class SpellCheckerImpl implements SpellChecker { @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); } } //subclass of SpellCheckerImpl class WinWordSpellCheckerImpl extends SpellCheckerImpl{ @Override public void checkSpelling() { System.out.println("Inside WinWordSpellCheckerImpl.checkSpelling." ); } }
Output
Compile and run the file, you will see the following output −
Inside WinWordSpellCheckerImpl.checkSpelling.
Guice - Binding Annotations
Introduction
As we can bind a type with its implementation. In case we want to map a type with multiple implmentations, we can create custom annotation as well. See the below example to understand the concept.
Create a binding annotation
@BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME) @interface WinWord {}
@BindingAnnotation − Marks annotation as binding annotation.
@Target − Marks applicability of annotation.
@Retention − Marks availablility of annotation as runtime.
Mapping using binding annotation
bind(SpellChecker.class).annotatedWith(WinWord.class).to(WinWordSpellCheckerImpl.class);
Inject using binding annotation
@Inject public TextEditor(@WinWord SpellChecker spellChecker) { this.spellChecker = spellChecker; }
Example - Usage of Binding Annotation
GuiceTester.java
package com.tutorialspoint; import java.lang.annotation.Target; import com.google.inject.AbstractModule; import com.google.inject.BindingAnnotation; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import java.lang.annotation.Retention; import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.METHOD; @BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME) @interface WinWord {} public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeSpellCheck(); } } class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor(@WinWord SpellChecker spellChecker) { this.spellChecker = spellChecker; } public void makeSpellCheck(){ spellChecker.checkSpelling(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { bind(SpellChecker.class).annotatedWith(WinWord.class) .to(WinWordSpellCheckerImpl.class); } } //spell checker interface interface SpellChecker { public void checkSpelling(); } //spell checker implementation class SpellCheckerImpl implements SpellChecker { @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); } } //subclass of SpellCheckerImpl class WinWordSpellCheckerImpl extends SpellCheckerImpl{ @Override public void checkSpelling() { System.out.println("Inside WinWordSpellCheckerImpl.checkSpelling." ); } }
Output
Compile and run the file, you will see the following output −
Inside WinWordSpellCheckerImpl.checkSpelling.
Guice - Named Binding
Introduction
Guice provides another way also to map bindings without creating a custom annoation. It allows so using @Named annotation.
Mapping using named annotation
bind(SpellChecker.class).annotatedWith(Names.named("OpenOffice")).to(OpenOfficeWordSpellCheckerImpl.class);
Inject using @Named annotation
@Inject public TextEditor(@Named("OpenOffice") SpellChecker spellChecker) { this.spellChecker = spellChecker; }
Example - Usage of Named Binding
GuiceTester.java
package com.tutorialspoint; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.name.Named; import com.google.inject.name.Names; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeSpellCheck(); } } class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor(@Named("OpenOffice") SpellChecker spellChecker) { this.spellChecker = spellChecker; } public void makeSpellCheck(){ spellChecker.checkSpelling(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { bind(SpellChecker.class).annotatedWith(Names.named("OpenOffice")) .to(OpenOfficeWordSpellCheckerImpl.class); } } //spell checker interface interface SpellChecker { public void checkSpelling(); } //spell checker implementation class SpellCheckerImpl implements SpellChecker { @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); } } //subclass of SpellCheckerImpl class OpenOfficeWordSpellCheckerImpl extends SpellCheckerImpl{ @Override public void checkSpelling() { System.out.println("Inside OpenOfficeWordSpellCheckerImpl.checkSpelling." ); } }
Output
Compile and run the file, you will see the following output −
Inside OpenOfficeWordSpellCheckerImpl.checkSpelling.
Guice - Constant Binding
Introduction
Guice provides a way to create bindings with value objects or constants. Consider the case where we want to configure JDBC url.
Inject using @Named annotation
@Inject public void connectDatabase(@Named("JBDC") String dbUrl) { //... }
This can be achived using toInstance() method.
bind(String.class).annotatedWith(Names.named("JBDC")).toInstance("jdbc:mysql://localhost:5326/emp");
Example - Usage of Binding with Constant String
GuiceTester.java
package com.tutorialspoint; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.name.Named; import com.google.inject.name.Names; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeConnection(); } } class TextEditor { private String dbUrl; @Inject public TextEditor(@Named("JDBC") String dbUrl) { this.dbUrl = dbUrl; } public void makeConnection(){ System.out.println(dbUrl); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { bind(String.class) .annotatedWith(Names.named("JDBC")) .toInstance("jdbc:mysql://localhost:5326/emp"); } }
Output
Compile and run the file, you will see the following output −
jdbc:mysql://localhost:5326/emp
Guice - @Provides Annotation
Introduction
Guice provides a way to create bindings with complex objects using @Provides annotation. This way, we can initialize the object before injecting as dependency.
@Provides public SpellChecker provideSpellChecker(){ String dbUrl = "jdbc:mysql://localhost:5326/emp"; String user = "user"; int timeout = 100; SpellChecker SpellChecker = new SpellCheckerImpl(dbUrl, user, timeout); return SpellChecker; }
This method is being part of Binding Module and provides the complex object to be mapped. See the complete example below.
Example - Usage of @Provides Annotation
GuiceTester.java
package com.tutorialspoint; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.Provides; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeSpellCheck(); } } class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor( SpellChecker spellChecker) { this.spellChecker = spellChecker; } public void makeSpellCheck(){ spellChecker.checkSpelling(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() {} @Provides public SpellChecker provideSpellChecker(){ String dbUrl = "jdbc:mysql://localhost:5326/emp"; String user = "user"; int timeout = 100; SpellChecker SpellChecker = new SpellCheckerImpl(dbUrl, user, timeout); return SpellChecker; } } //spell checker interface interface SpellChecker { public void checkSpelling(); } //spell checker implementation class SpellCheckerImpl implements SpellChecker { private String dbUrl; private String user; private Integer timeout; @Inject public SpellCheckerImpl(String dbUrl, String user, Integer timeout){ this.dbUrl = dbUrl; this.user = user; this.timeout = timeout; } @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); System.out.println(dbUrl); System.out.println(user); System.out.println(timeout); } }
Output
Compile and run the file, you will see the following output −
Inside checkSpelling. jdbc:mysql://localhost:5326/emp user 100
Guice - Provider Interface
Introduction
Using @provides annotation of multiple methods can become more complex when code base become large in size. We can move these methods to seperate classes by implementing the Provider interface as shown below −
class SpellCheckerProvider implements Provider<SpellChecker>{ @Override public SpellChecker get() { String dbUrl = "jdbc:mysql://localhost:5326/emp"; String user = "user"; int timeout = 100; SpellChecker SpellChecker = new SpellCheckerImpl(dbUrl, user, timeout); return SpellChecker; } }
Next step is to map the provider to type.
bind(SpellChecker.class).toProvider(SpellCheckerProvider.class);
See the complete example below.
Example - Usage of Provider Interface
GuiceTester.java
package com.tutorialspoint; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.Provider; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeSpellCheck(); } } class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor( SpellChecker spellChecker) { this.spellChecker = spellChecker; } public void makeSpellCheck(){ spellChecker.checkSpelling(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { bind(SpellChecker.class) .toProvider(SpellCheckerProvider.class); } } //spell checker interface interface SpellChecker { public void checkSpelling(); } //spell checker implementation class SpellCheckerImpl implements SpellChecker { private String dbUrl; private String user; private Integer timeout; @Inject public SpellCheckerImpl(String dbUrl, String user, Integer timeout){ this.dbUrl = dbUrl; this.user = user; this.timeout = timeout; } @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); System.out.println(dbUrl); System.out.println(user); System.out.println(timeout); } } class SpellCheckerProvider implements Provider<SpellChecker>{ @Override public SpellChecker get() { String dbUrl = "jdbc:mysql://localhost:5326/emp"; String user = "user"; int timeout = 100; SpellChecker SpellChecker = new SpellCheckerImpl(dbUrl, user, timeout); return SpellChecker; } }
Output
Compile and run the file, you will see the following output −
Inside checkSpelling. jdbc:mysql://localhost:5326/emp user 100
Guice - Constructor Bindings
Introduction
Guice provides a way to create bindings with specific constructor of an object using toConstructor() method.
@Override protected void configure() { try { bind(SpellChecker.class) .toConstructor(SpellCheckerImpl.class.getConstructor(String.class)); } catch (NoSuchMethodException | SecurityException e) { System.out.println("Required constructor missing"); } }
See the complete example below.
Example - Usage of Constructor based Binding
GuiceTester.java
package com.tutorialspoint; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.name.Named; import com.google.inject.name.Names; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeSpellCheck(); } } class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor( SpellChecker spellChecker) { this.spellChecker = spellChecker; } public void makeSpellCheck(){ spellChecker.checkSpelling(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { try { bind(SpellChecker.class) .toConstructor(SpellCheckerImpl.class.getConstructor(String.class)); } catch (NoSuchMethodException | SecurityException e) { System.out.println("Required constructor missing"); } bind(String.class) .annotatedWith(Names.named("JDBC")) .toInstance("jdbc:mysql://localhost:5326/emp"); } } //spell checker interface interface SpellChecker { public void checkSpelling(); } //spell checker implementation class SpellCheckerImpl implements SpellChecker { private String dbUrl; public SpellCheckerImpl(){} public SpellCheckerImpl(@Named("JDBC") String dbUrl){ this.dbUrl = dbUrl; } @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); System.out.println(dbUrl); } }
Output
Compile and run the file, you will see the following output −
Inside checkSpelling. jdbc:mysql://localhost:5326/emp
Guice - Inbuilt Bindings
Introduction
Guice provides inbuilt binding for java.util.logging.Logger class. Logger's name is automatically set to the name of the class into which the Logger is injected.
class TextEditor { private Logger logger; @Inject public TextEditor( Logger logger) { this.logger = logger; } public void makeSpellCheck(){ logger.info("In TextEditor.makeSpellCheck() method"); } }
See the complete example below.
Example - Usage of Inbuilt Binding
GuiceTester.java
package com.tutorialspoint; import java.util.logging.Logger; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeSpellCheck(); } } class TextEditor { private Logger logger; @Inject public TextEditor( Logger logger) { this.logger = logger; } public void makeSpellCheck(){ logger.info("In TextEditor.makeSpellCheck() method"); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { } }
Output
Compile and run the file, you will see the following output −
Aug 20, 2025 11:39:57 AM com.tutorialspoint.TextEditor makeSpellCheck INFO: In TextEditor.makeSpellCheck() method
Guice - Just-In-Time Bindings
Introduction
As bindings are defined in Binding Module, Guice uses them whenever it needs to inject dependencies. In case bindings are not present, it can attempt to create just-in-time bindings. Bindings present in binding module are called explicit bindings and are of higher precedence whereas just-in-time bindings are called implicit bindings. If both type of bindings are present, explicit bindings are considered for mapping.
Types of Just-In-Time Bindings
Following are the examples of three types of Just-in-time bindings.
Binding Type | Description |
---|---|
Injectable Constructors | Non-private, No-argument constructors are eligible for just-in-time bindings. Another way is to annotate a constructor with @Inject annotation. |
@ImplementedBy annotation | @ImplementedBy annotation tells the guice about the implementation class. No binding is required in Binding Module in such a case. |
@ProvidedBy annotation | @ProvidedBy annotation tells the guice about the provider of implementation class. No binding is required in Binding Module in such a case. |
Guice - Constructor Injection
Introduction
Injection is a process of injecting dependency into an object. Constructor injection is quite common. In this process, dependency is injected as argument to the constructor. See the example below −
class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor(SpellChecker spellChecker) { this.spellChecker = spellChecker; } ... }
Example - Usage of Constructor Injection
GuiceTester.java
package com.tutorialspoint; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeSpellCheck(); } } class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor(SpellChecker spellChecker) { this.spellChecker = spellChecker; } public void makeSpellCheck(){ spellChecker.checkSpelling(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { bind(SpellChecker.class).to(SpellCheckerImpl.class); } } //spell checker interface interface SpellChecker { public void checkSpelling(); } //spell checker implementation class SpellCheckerImpl implements SpellChecker { @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); } }
Output
Compile and run the file, you will see the following output −
Inside checkSpelling.
Guice - Method Injection
Introduction
Injection is a process of injecting dependency into an object. Method injection is used to set value object as dependency to the object. See the example below −
//spell checker implementation class SpellCheckerImpl implements SpellChecker { private String dbUrl; public SpellCheckerImpl(){} @Inject public void setDbUrl(@Named("JDBC") String dbUrl){ this.dbUrl = dbUrl; } ... }
Example - Usage of Method Injection
GuiceTester.java
package com.tutorialspoint; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.ImplementedBy; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.name.Named; import com.google.inject.name.Names; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeSpellCheck(); } } class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor( SpellChecker spellChecker) { this.spellChecker = spellChecker; } public void makeSpellCheck(){ spellChecker.checkSpelling(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { bind(String.class) .annotatedWith(Names.named("JDBC")) .toInstance("jdbc:mysql://localhost:5326/emp"); } } @ImplementedBy(SpellCheckerImpl.class) interface SpellChecker { public void checkSpelling(); } //spell checker implementation class SpellCheckerImpl implements SpellChecker { private String dbUrl; public SpellCheckerImpl(){} @Inject public void setDbUrl(@Named("JDBC") String dbUrl){ this.dbUrl = dbUrl; } @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); System.out.println(dbUrl); } }
Output
Compile and run the file, you will see the following output −
Inside checkSpelling. jdbc:mysql://localhost:5326/emp
Guice - Field Injection
Introduction
Injection is a process of injecting dependency into an object. Field injection is used to set value object as dependency to the field of an object. See the example below −
//spell checker implementation class SpellCheckerImpl implements SpellChecker { @Inject @Named("JDBC") private String dbUrl; ... }
Example - Usage of Field Injection
GuiceTester.java
package com.tutorialspoint; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.ImplementedBy; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.name.Named; import com.google.inject.name.Names; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeSpellCheck(); } } class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor( SpellChecker spellChecker) { this.spellChecker = spellChecker; } public void makeSpellCheck(){ spellChecker.checkSpelling(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { bind(String.class) .annotatedWith(Names.named("JDBC")) .toInstance("jdbc:mysql://localhost:5326/emp"); } } @ImplementedBy(SpellCheckerImpl.class) interface SpellChecker { public void checkSpelling(); } //spell checker implementation class SpellCheckerImpl implements SpellChecker { @Inject @Named("JDBC") private String dbUrl; public SpellCheckerImpl(){} @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); System.out.println(dbUrl); } }
Output
Compile and run the file, you will see the following output −
Inside checkSpelling. jdbc:mysql://localhost:5326/emp
Guice - Optional Injection
Introduction
Injection is a process of injecting dependeny into an object. Optional injection means injecting the dependency if exists. Method and Field injections may be optionally dependent and should have some default value if dependency is not present. See the example below −
//spell checker implementation class SpellCheckerImpl implements SpellChecker { private String dbUrl = "jdbc:mysql://localhost:5326/emp"; public SpellCheckerImpl(){} @Inject(optional=true) public void setDbUrl(@Named("JDBC") String dbUrl){ this.dbUrl = dbUrl; } ... }
Example - Usage of Optional Injection
GuiceTester.java
package com.tutorialspoint; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.ImplementedBy; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.name.Named; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeSpellCheck(); } } class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor( SpellChecker spellChecker) { this.spellChecker = spellChecker; } public void makeSpellCheck(){ spellChecker.checkSpelling(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() {} } @ImplementedBy(SpellCheckerImpl.class) interface SpellChecker { public void checkSpelling(); } //spell checker implementation class SpellCheckerImpl implements SpellChecker { private String dbUrl = "jdbc:mysql://localhost:5326/emp"; public SpellCheckerImpl(){} @Inject(optional=true) public void setDbUrl(@Named("JDBC") String dbUrl){ this.dbUrl = dbUrl; } @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); System.out.println(dbUrl); } }
Output
Compile and run the file, you will see the following output −
Inside checkSpelling. jdbc:mysql://localhost:5326/emp
Guice - On Demand Injection
Introduction
Injection is a process of injecting dependency into an object. Method and field injections can be used to initialize with existing object using injector.injectMembers() method. See the example below −
Injector injector = Guice.createInjector(new TextEditorModule()); SpellChecker spellChecker = new SpellCheckerImpl(); injector.injectMembers(spellChecker);
Example - Usage of On Demand Injection
GuiceTester.java
package com.tutorialspoint; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.ImplementedBy; import com.google.inject.Inject; import com.google.inject.Injector; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); SpellChecker spellChecker = new SpellCheckerImpl(); injector.injectMembers(spellChecker); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeSpellCheck(); } } class TextEditor { private SpellChecker spellChecker; @Inject public void setSpellChecker(SpellChecker spellChecker){ this.spellChecker = spellChecker; } public TextEditor() { } public void makeSpellCheck(){ spellChecker.checkSpelling(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { } } @ImplementedBy(SpellCheckerImpl.class) interface SpellChecker { public void checkSpelling(); } //spell checker implementation class SpellCheckerImpl implements SpellChecker { public SpellCheckerImpl(){} @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); } }
Output
Compile and run the file, you will see the following output −
Inside checkSpelling.
Guice - Scopes
Introduction
Guice returns a new instance every time when it supplies a value as its default behaviour. It is configurable via scopes. Following are the scopes that Guice supports:
@Singleton − Single instance for lifetime of the application. @Singleton object needs to be threadsafe.
@SessionScoped − Single instance for a particular session of the web application. @SessionScoped object needs to be threadsafe.
@RequestScoped − Single instance for a particular request of the web application. @RequestScoped object does not need to be threadsafe.
Way to apply scopes.
Following are the ways to apply scopes.
At Class level
@Singleton class SpellCheckerImpl implements SpellChecker { public SpellCheckerImpl(){} @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); } }
At Configuration level
bind(SpellChecker.class).to(SpellCheckerImpl.class).in(Singleton.class);
At Method level
@Provides @Singleton public SpellChecker provideSpellChecker(){ String dbUrl = "jdbc:mysql://localhost:5326/emp"; String user = "user"; int timeout = 100; SpellChecker SpellChecker = new SpellCheckerImpl(dbUrl, user, timeout); return SpellChecker; }
Let's see the Scope at class level in action.
Example - Usage of @Singleton Annotation
GuiceTester.java
package com.tutorialspoint; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.Singleton; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); SpellChecker spellChecker = new SpellCheckerImpl(); injector.injectMembers(spellChecker); TextEditor editor = injector.getInstance(TextEditor.class); System.out.println(editor.getSpellCheckerId()); TextEditor editor1 = injector.getInstance(TextEditor.class); System.out.println(editor1.getSpellCheckerId()); } } class TextEditor { private SpellChecker spellChecker; @Inject public void setSpellChecker(SpellChecker spellChecker){ this.spellChecker = spellChecker; } public TextEditor() { } public void makeSpellCheck(){ spellChecker.checkSpelling(); } public double getSpellCheckerId(){ return spellChecker.getId(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { bind(SpellChecker.class).to(SpellCheckerImpl.class); } } interface SpellChecker { public double getId(); public void checkSpelling(); } @Singleton class SpellCheckerImpl implements SpellChecker { double id; public SpellCheckerImpl(){ id = Math.random(); } @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); } @Override public double getId() { return id; } }
Output
Compile and run the file, you may see the following output with same numbers −
0.4168185515418945 0.4168185515418945
Example - Without using @Singleton Annotation
GuiceTester.java
package com.tutorialspoint; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); SpellChecker spellChecker = new SpellCheckerImpl(); injector.injectMembers(spellChecker); TextEditor editor = injector.getInstance(TextEditor.class); System.out.println(editor.getSpellCheckerId()); TextEditor editor1 = injector.getInstance(TextEditor.class); System.out.println(editor1.getSpellCheckerId()); } } class TextEditor { private SpellChecker spellChecker; @Inject public void setSpellChecker(SpellChecker spellChecker){ this.spellChecker = spellChecker; } public TextEditor() { } public void makeSpellCheck(){ spellChecker.checkSpelling(); } public double getSpellCheckerId(){ return spellChecker.getId(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { bind(SpellChecker.class).to(SpellCheckerImpl.class); } } interface SpellChecker { public double getId(); public void checkSpelling(); } class SpellCheckerImpl implements SpellChecker { double id; public SpellCheckerImpl(){ id = Math.random(); } @Override public void checkSpelling() { System.out.println("Inside checkSpelling." ); } @Override public double getId() { return id; } }
Output
Compile and run the file, you may see the following output with different numbers −
0.8917278158910874 0.7214864684379797
Guice - AOP
Introduction
AOP, Aspect oriented programming entails breaking down program logic into distinct parts called so-called concerns. The functions that span multiple points of an application are called cross-cutting concerns and these cross-cutting concerns are conceptually separate from the application's business logic. There are various common good examples of aspects like logging, auditing, declarative transactions, security, caching, etc.
The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Dependency Injection helps you decouple your application objects from each other and AOP helps you decouple cross-cutting concerns from the objects that they affect. AOP is like triggers in programming languages such as Perl, .NET, Java, and others. Guice provides interceptors to intercept an application. For example, when a method is executed, you can add extra functionality before or after the method execution.
Important Classes
Matcher − Matcher is an interface to either accept or reject a value. In Guice AOP, we need two matchers: one to define which classes participate, and another for the methods of those classes.
MethodInterceptor − MethodInterceptors are executed when a matching method is called. They can inspect the call: the method, its arguments, and the receiving instance. We can perform cross-cutting logic and then delegate to the underlying method. Finally, we may inspect the return value or exception and return.
Example - AOP implementation
GuiceTester.java
package com.tutorialspoint; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; import com.google.inject.AbstractModule; import com.google.inject.Guice; import com.google.inject.Inject; import com.google.inject.Injector; import com.google.inject.matcher.Matchers; public class GuiceTester { public static void main(String[] args) { Injector injector = Guice.createInjector(new TextEditorModule()); TextEditor editor = injector.getInstance(TextEditor.class); editor.makeSpellCheck(); } } class TextEditor { private SpellChecker spellChecker; @Inject public TextEditor(SpellChecker spellChecker) { this.spellChecker = spellChecker; } public void makeSpellCheck(){ spellChecker.checkSpelling(); } } //Binding Module class TextEditorModule extends AbstractModule { @Override protected void configure() { bind(SpellChecker.class).to(SpellCheckerImpl.class); bindInterceptor(Matchers.any(), Matchers.annotatedWith(CallTracker.class), new CallTrackerService()); } } //spell checker interface interface SpellChecker { public void checkSpelling(); } //spell checker implementation class SpellCheckerImpl implements SpellChecker { @Override @CallTracker public void checkSpelling() { System.out.println("Inside checkSpelling." ); } } @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @interface CallTracker {} class CallTrackerService implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { System.out.println("Before " + invocation.getMethod().getName()); Object result = invocation.proceed(); System.out.println("After " + invocation.getMethod().getName()); return result; } }
Output
Compile and run the file, you may see the following output −
Before checkSpelling Inside checkSpelling. After checkSpelling