about the tutorial · google guice 5 set classpath variable set the classpath environment variable...

55
i

Upload: others

Post on 24-Aug-2020

29 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

i

Page 2: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

i

About the Tutorial

Guice is an open source, Java-based dependency injection framework. It is quiet

lightweight and is actively developed/managed by Google. This tutorial covers the

concepts required for a basic understanding of Google Guice and gives you a feel of how

it works.

This tutorial gives you a comprehensive coverage of concepts of Guice and makes you

comfortable to use it in your software development projects.

Audience

This tutorial has been prepared for the beginners to help them understand the basic to

advanced concepts related to Google Guice.

Prerequisites

Before you start practicing various types of examples given in this reference, we assume

that you are already aware about computer programs and computer programming

languages.

Copyright & Disclaimer

@Copyright 2018 by Tutorials Point (I) Pvt. Ltd.

All the content and graphics published in this e-book are the property of Tutorials Point (I)

Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish

any contents or a part of contents of this e-book in any manner without written consent

of the publisher.

We strive to update the contents of our website and tutorials as timely and as precisely as

possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.

Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our

website or its contents including this tutorial. If you discover any errors on our website or

in this tutorial, please notify us at [email protected]

Page 3: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

ii

Table of Contents

About the Tutorial ................................................................................................................................ i

Audience ............................................................................................................................................... i

Prerequisites ......................................................................................................................................... i

Copyright & Disclaimer ......................................................................................................................... i

Table of Contents ................................................................................................................................. ii

1. GUICE – OVERVIEW .......................................................................................................... 1

Dependency Injection .......................................................................................................................... 1

Dependency Injection using Guice (Binding) ........................................................................................ 2

2. GUICE – ENVIRONMENTAL SETUP .................................................................................... 3

Local Environment Setup ..................................................................................................................... 3

Setting up the Path for Windows 2000/XP ........................................................................................... 3

Setting up the Path for Windows 95/98/ME ........................................................................................ 3

Setting up the Path for Linux, UNIX, Solaris, FreeBSD .......................................................................... 3

Popular Java Editors............................................................................................................................. 4

Google Guice Environment .................................................................................................................. 4

Set CLASSPATH Variable ...................................................................................................................... 5

3. GOOGLE GUICE – FIRST APPLICATION .............................................................................. 6

Complete Example ............................................................................................................................... 7

4. GOOGLE GUICE – LINKED BINDING ................................................................................. 10

Complete Example ............................................................................................................................. 10

5. GOOGLE GUICE – BINDING ANNOTATIONS .................................................................... 12

Complete Example ............................................................................................................................. 12

6. GOOGLE GUICE – NAMED BINDING ................................................................................ 15

Complete Example ............................................................................................................................. 15

Page 4: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

iii

7. GOOGLE GUICE – CONSTANT BINDINGS ........................................................................ 18

Complete Example ............................................................................................................................. 18

8. GOOGLE GUICE – @PROVIDES ANNOTATION ............................................................... 20

Complete Example ............................................................................................................................. 20

9. GOOGLE GUICE – PROVIDER CLASS ................................................................................ 23

10. GOOGLE GUICE – CONSTRUCTOR BINDINGS .................................................................. 26

11. GOOGLE GUICE – INBUILT BINDINGS .............................................................................. 29

12. GOOGLE GUICE – JUST-IN-TIME BINDINGS ..................................................................... 31

13. GOOGLE GUICE – CONSTRUCTOR INJECTION ................................................................. 33

14. GOOGLE GUICE – METHOD INJECTION ........................................................................... 35

15. GOOGLE GUICE – FIELD INJECTION ................................................................................. 37

16. GOOGLE GUICE – OPTIONAL INJECTION ......................................................................... 39

17. GOOGLE GUICE – ON-DEMAND INJECTION .................................................................... 41

18. GOOGLE GUICE – SCOPES ............................................................................................... 44

Application of Scopes ........................................................................................................................ 44

Example ............................................................................................................................................. 45

19. GOOGLE GUICE – ASPECT ORIENTED PROGRAMMING ................................................... 49

Important Classes .............................................................................................................................. 49

Example ............................................................................................................................................. 49

Page 5: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

1

Guice is an open source, Java-based dependency injection framework. It is lightweight and

is developed as well as managed by Google. This chapter will give you an overview of

Guice framework.

Dependency Injection

Any Java-based application contains objects that work together to present what the end-

user sees as a final working application. When writing a complex Java application,

application classes should be independent of other Java classes as much possible to

increase their reusability. This also makes them independent of other classes while unit

testing. Dependency Injection, also termed as 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();

}

}

Note that here we have created 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 is not concerned about SpellChecker implementation. The

SpellChecker will be implemented independently and will be provided to the TextEditor at

the time of TextEditor instantiation.

1. Guice – Overview

Page 6: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

2

Dependency Injection using Guice (Binding)

Dependency Injection is controlled by the Guice Bindings. Guice uses bindings to map

object types to their actual implementations. 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);

}

}

An injector is the object-graph builder and a Module is its core building block. Thus, the

first step is to create an injector and then 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 the above example, TextEditor class object graph is constructed by Guice and this graph

contains TextEditor object and its dependency as WinWordSpellChecker object.

Page 7: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

3

Let us look into setting up the environment for Guice, before proceeding into its

functionalities.

Local Environment Setup

This section guides you on how to download and set up Java environment on your machine.

Please follow the steps mentioned below to set up the environment.

Java SE is freely available from the link Download Java. Download a version based on your

operating system. Then, follow the instructions to download Java and run the .exe file to

install Java on your machine. Once you installed Java, you would need to set environment

variables to point to correct installation directories.

Setting up the Path for Windows 2000/XP

We are assuming that you have installed Java in c:\Program Files\java\jdkdirectory −

Right-click 'My Computer' and select 'Properties'.

Click the 'Environment variables' button under the 'Advanced' tab.

Now, alter the 'Path' variable so that it also contains the path to the Java

executable. For example, if the path is currently set to

'C:\WINDOWS\SYSTEM32', then change your path to read

'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.

Setting up the Path for Windows 95/98/ME

In case, you have installed Java in c:\Program Files\java\jdkdirectory, edit the

'C:\autoexec.bat' file and add the following line at the end: 'SET

PATH=%PATH%;C:\Program Files\java\jdk\bin'.

Setting up the Path for Linux, UNIX, Solaris, FreeBSD

Environment variable PATH should be set to point to where the Java binaries have been

installed. You may refer to your shell documentation for more information.

For example, if you use bash as your shell, then you would add the following line to the

end: '.bashrc: export PATH=/path/to/java:$PATH'

2. Guice – Environmental Setup

Page 8: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

4

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, you can use any simple text editor like Notepad

(Recommended for this tutorial), or TextPad.

Netbeans: It is a Java IDE that is open-source and free, which can be downloaded

from https://www.netbeans.org/index.html.

Eclipse: It is also a Java IDE developed by the Eclipse open-source community and

can be downloaded from https://www.eclipse.org/.

Google Guice Environment

Download the latest version of Google Guice and related jar files.

Google Guice 4.0

AOP Alliance 1.0

Guava 16.0.1

javax.inject 1.0

Note that in this tutorial, we have copied all these into C:\>Google folder.

OS Archive name

Windows guice-4.1.0.jar;aopalliance-1.0.jar;guava-16.0.1.jar;javax.inject-1.jar

Linux guice-4.1.0.jar;aopalliance-1.0.jar;guava-16.0.1.jar;javax.inject-1.jar

Mac guice-4.1.0.jar;aopalliance-1.0.jar;guava-16.0.1.jar;javax.inject-1.jar

Page 9: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

5

Set CLASSPATH Variable

Set the CLASSPATH environment variable to point to the Guice jar location, assuming

you have stored Guice and the related jars in Google folder on various Operating Systems

as follows.

OS Output

Windows Set the environment variable CLASSPATH to

%CLASSPATH%;C:\Google\guice-4.1.0.jar;C:\Google\aopalliance-

1.0.jar;C:\Google\guava-16.0.1.jar;C:\Google\javax.inject-1.jar;.;

Linux export CLASSPATH=$CLASSPATH:Google/guice-

4.1.0.jar:Google/aopalliance-1.0.jar:Google/guava-

16.0.1.jar:Google/javax.inject-1.jar:.

Mac export CLASSPATH=$CLASSPATH:Google/guice-

4.1.0.jar:Google/aopalliance-1.0.jar:Google/guava-

16.0.1.jar:Google/javax.inject-1.jar:.

Page 10: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

6

In this chapter, let us look into a sample console based application where well demonstrate

dependency injection using Guice binding mechanism step by step, with their respective

code.

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) {

3. Google Guice – First Application

Page 11: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

7

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();

Complete Example

Create a java class named GuiceTester.

GuiceTester.java

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) {

Page 12: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

8

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

Now, compile and run the file. You can see the following output:

Inside checkSpelling.

Page 13: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

9

Bindings Examples

Page 14: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

10

In Linked bindings, Guice maps a type to its implementation. In example discussed below,

we have mapped SpellChecker interface with its implementation SpellCheckerImpl.

bind(SpellChecker.class).to(SpellCheckerImpl.class);

We can also map the concrete class to its subclass as shown below:

bind(SpellCheckerImpl.class).to(WinWordSpellCheckerImpl.class);

Observe that here we have chained the bindings. Let us see the result in complete

example.

Complete Example

Create a java class named GuiceTester.

GuiceTester.java

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();

}

4. Google Guice – Linked Binding

Page 15: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

11

}

//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

Now, compile and run the file. You can see the following output:

Inside WinWordSpellCheckerImpl.checkSpelling.

Page 16: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

12

As we can bind a type with its implementation. In case we want to map a type with multiple

implementations, 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 availability of annotation as runtime.

Mapping using binding annotation

bind(SpellChecker.class).annotatedWith(WinWord.class).to(WinWordSpellCheckerImp

l.class);

Inject using binding annotation

@Inject

public TextEditor(@WinWord SpellChecker spellChecker) {

this.spellChecker = spellChecker;

}

Complete Example

Create a java class named GuiceTester.

GuiceTester.java

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;

5. Google Guice – Binding Annotations

Page 17: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

13

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);

}

}

Page 18: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

14

//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

Now, compile and run the file. You can see the following output:

Inside WinWordSpellCheckerImpl.checkSpelling.

Page 19: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

15

Guice provides another way also to map bindings without creating a custom annotation.

It allows so using @Named annotation.

Mapping using named annotation

bind(SpellChecker.class).annotatedWith(Names.named("OpenOffice")).to(OpenOffice

WordSpellCheckerImpl.class);

Inject using @Named annotation

@Inject

public TextEditor(@Named("OpenOffice") SpellChecker spellChecker) {

this.spellChecker = spellChecker;

}

Complete Example

Create a java class named GuiceTester.

GuiceTester.java

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

6. Google Guice – Named Binding

Page 20: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

16

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."

);

} }

Page 21: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

17

Output

Now, compile and run the file. You can see the following output:

Inside OpenOfficeWordSpellCheckerImpl.checkSpelling.

Page 22: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

18

Guice provides a way to create bindings with value objects or constants. Let us 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 achieved using toInstance() method.

bind(String.class).annotatedWith(Names.named("JBDC")).toInstance("jdbc:mysql://

localhost:5326/emp");

Complete Example

Create a java class named GuiceTester.

GuiceTester.java

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

7. Google Guice – Constant Bindings

Page 23: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

19

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

Now, compile and run the file. You can see the following output:

jdbc:mysql://localhost:5326/emp

Page 24: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

20

Guice provides a way to create bindings with complex objects using @provides method.

@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 a part of Binding Module and provides the complex object to be mapped.

See the complete example below.

Complete Example

Create a java class named GuiceTester.

GuiceTester.java

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

8. Google Guice – @Provides Annotation

Page 25: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

21

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

Page 26: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

22

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

Now, compile and run the file. You can see the following output:

Inside checkSpelling.

jdbc:mysql://localhost:5326/emp

user

100

Page 27: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

23

As @provides method becomes more complex, this method can be moved to separate

classes using Provider interface.

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, you have to map the provider to type.

bind(SpellChecker.class).toProvider(SpellCheckerProvider.class);

See the complete example below.

Complete Example

Create a java class named GuiceTester.

GuiceTester.java

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();

}

}

9. Google Guice – Provider Class

Page 28: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

24

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) {

Page 29: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

25

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

Now, compile and run the file. You can see the following output:

Inside checkSpelling.

jdbc:mysql://localhost:5326/emp

user

100

Page 30: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

26

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.

Complete Example

Create a java class named GuiceTester.

GuiceTester.java

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;

10. Google Guice – Constructor Bindings

Page 31: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

27

@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(){}

Page 32: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

28

public SpellCheckerImpl(@Named("JDBC") String dbUrl) {

this.dbUrl = dbUrl;

}

@Override

public void checkSpelling() {

System.out.println("Inside checkSpelling." );

System.out.println(dbUrl);

}

}

Output

Now, compile and run the file. You can see the following output:

Inside checkSpelling.

jdbc:mysql://localhost:5326/emp

Page 33: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

29

Guice provides an inbuilt binding for java.util.logging.Logger class. The logger's name

is automatically set to the name of the class into which the Logger is injected. Observe

the example given below.

Example

Create a java class named GuiceTester.

GuiceTester.java

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

11. Google Guice – Inbuilt Bindings

Page 34: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

30

class TextEditorModule extends AbstractModule {

@Override

protected void configure() {

}

}

Output

Now, compile and run the file. You can see the following output:

Dec 20, 2017 12:51:05 PM TextEditor makeSpellCheck

INFO: In TextEditor.makeSpellCheck() method

Page 35: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

31

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 the binding module are called Explicit bindings and are of

higher precedence whereas just-in-time bindings are called Implicit bindings. Note that

if both type of bindings are present, explicit bindings are considered for mapping.

The examples for three types of Just-in-time bindings are given below:

Sr.No. Binding Type & Description

1 Injectable Constructors

Non-private, No-argument constructors are eligible for just-in-time bindings.

Another way is to annotate a constructor with @Inject annotation.

2 @ImplementatedBy annotation

@ImplementatedBy annotation tells the guice about the implementation class.

No binding is required in Binding Module in such a case.

3 @ProvidedBy annotation

@ProvidedBy annotation tells the guice about the provider of implementation

class. No binding is required in Binding Module in such a case.

12. Google Guice – Just-in-time Bindings

Page 36: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

32

Injection Examples

Page 37: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

33

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.

Example

Create a java class named GuiceTester.

GuiceTester.java

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

13. Google Guice – Constructor Injection

Page 38: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

34

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

Now, compile and run the file. You can see the following output:

Inside checkSpelling.

Page 39: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

35

Injection is a process of injecting dependency into an object. Method injection is used to

set value object as dependency to the object. Observe the example given below.

Example

Create a java class named GuiceTester.

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

14. Google Guice – Method Injection

Page 40: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

36

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

Now, compile and run the file. You can see the following output:

Inside checkSpelling.

jdbc:mysql://localhost:5326/emp

Page 41: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

37

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. Observe the example given below.

Example

Create a java class named GuiceTester.

GuiceTester.java

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

15. Google Guice – Field Injection

Page 42: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

38

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

Now, compile and run the file. You can see the following output:

Inside checkSpelling.

jdbc:mysql://localhost:5326/emp

Page 43: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

39

Injection is a process of injecting dependency 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.

Example

Create a java class named GuiceTester.

GuiceTester.java

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();

}

}

16. Google Guice – Optional Injection

Page 44: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

40

//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

Now, compile and run the file. You can see the following output:

Inside checkSpelling.

jdbc:mysql://localhost:5326/emp

Page 45: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

41

Injection is a process of injecting dependency into an object. Method and field injections

can be used to initialize using exiting object using injector.injectMembers() method.

See the example below.

Example

Create a java class named GuiceTester.

GuiceTester.java

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();

17. Google Guice – On-Demand Injection

Page 46: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

42

}

}

//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." );

}

}

Now, compile and run the file. You can see the following output:

Inside checkSpelling.

Page 47: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

43

Miscellaneous Examples

Page 48: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

44

Guice returns a new instance every time it supplies a value as its default behavior. It is

configurable via scopes. The various scopes that Guice supports are:

@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 need not be threadsafe.

Application of Scopes

You can apply scopes in the following ways:

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;

18. Google Guice – Scopes

Page 49: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

45

SpellChecker SpellChecker = new SpellCheckerImpl(dbUrl, user, timeout);

return SpellChecker;

}

Example

Let us see the scope at class level in action.

Result with @Singleton Annotation

Create a java class named GuiceTester.

GuiceTester.java

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;

}

Page 50: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

46

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;

}

}

Now, compile and run the file, you can see the following output with same numbers.

Page 51: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

47

0.3055839187063575

0.3055839187063575

Result without @Singleton Annotation

Create a java class named GuiceTester.

GuiceTester.java

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();

}

Page 52: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

48

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;

}

}

Now, compile and run the file. You can see the following output with different numbers:

0.556007079571739

0.22095011760351602

Page 53: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

49

AOP, Aspect oriented programming entails breaking down the program logic into distinct

parts 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 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

Create a java class named GuiceTester.

GuiceTester.java

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;

19. Google Guice – Aspect Oriented Programming

Page 54: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

50

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();

}

Page 55: About the Tutorial · Google Guice 5 Set CLASSPATH Variable Set the CLASSPATH environment variable to point to the Guice jar location, assuming you have stored Guice and the related

Google Guice

51

//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;

}

}

Now, compile and run the file. You can see the following output:

Before checkSpelling

Inside checkSpelling.

After checkSpelling