In this tutorial, we will explain the basic injection methods of Guice. This simple tutorial allows us to quickly use Guice for simple and systematic development. We will go into more modules in the future.

Based on the environment


technology version
Java 1.8 +
Guice holdings

Initialize the project


  • Initialize the project
mvn archetype:generate -DgroupId=io.edurt.lc.guice -DartifactId=guice-binder-basic - DarchetypeArtifactId = maven archetype - quickstart - Dversion = 1.0.0 - DinteractiveMode = falseCopy the code
  • Modify pom.xml to add Guice dependency
<? The XML version = "1.0" encoding = "utf-8"? > < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > < the parent > < artifactId > lc - guice < / artifactId > < groupId > io.edu rt. Lc. Guice < / groupId > < version > 1.0.0 < / version > < / parent > <modelVersion>4.0.0</modelVersion> <artifactId> Guice-binder-Basic </artifactId> <name>Learning Center for Guice Binder (Basic) < / name > < properties > < maven.com piler. Source > 1.8 < / maven.com piler source > < maven.com piler target > 1.8 < / maven.com piler. Target > < / properties > < dependencies > < the dependency > < the groupId > com. Google. Inject < / groupId > < artifactId > guice < / artifactId > < version > holdings < / version > < / dependency > </dependencies> </project>Copy the code

Guice: Guice is the dependency that our core uses

Guice’s binding model


  • Modify thepom.xmlConfiguration file, independenciesAdd the following content to the node
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
Copy the code
  • insrc/main/javaCreating a Directoryio.edurt.lc.guiceDirectory and create a new directory under the directoryGuiceBasicModuleClass file, enter the following content in the file
package io.edurt.lc.guice;

import com.google.inject.AbstractModule;

public class GuiceBasicModule
        extends AbstractModule
{

    @Override
    protected void configure()
    {
        System.out.println("Hello, GuiceBasicModule");
    }
}
Copy the code

The binding model in Guice is as simple as the one in Spring, and we can provide our program with arbitrary injected classes through bindings.

Binding the Module we need only needs to inherit the Guice com. Google, inject. AbstractModule can, in the configure method to realize the binding information we need.

  • insrc/test/javaBuild in the source directoryio.edurt.lc.guice.TestGuiceBasicModuleEnter the following in the unit test class file that we use to test our code
package io.edurt.lc.guice;

import com.google.inject.AbstractModule;

public class GuiceBasicModule
        extends AbstractModule
{

    @Override
    protected void configure()
    {
        System.out.println("Hello, GuiceBasicModule");
    }
}
Copy the code

After running the unit test, the console prints the following:

Hello, GuiceBasicModule
Copy the code

Or use the maven command MVN clean package, which will output something like the following

. ------------------------------------------------------- T E S T S ------------------------------------------------------- Running io.edurt.lc.guice.TestGuiceBasicModule Hello, GuiceBasicModule Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.094 SEC Results: Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 ......Copy the code

The custom Class


  • insrc/main/javaCreating a Directoryio.edurt.lc.guice.GuiceBasicServiceThe content of the class file is as follows
package io.edurt.lc.guice;

public interface GuiceBasicService
{
    void print(String input);
}
Copy the code

We define an interface file that builds a print method for information output.

  • insrc/main/javaCreating a Directoryio.edurt.lc.guice.GuiceBasicServiceImplThe content of the class file is as follows
package io.edurt.lc.guice; public class GuiceBasicServiceImpl implements GuiceBasicService { @Override public void print(String input) { System.out.println(String.format("print %s", input)); }}Copy the code

It implements the io.edu rt. Lc. Guice. GuiceBasicService print data function in the class

  • Modify thesrc/main/javadirectoryio.edurt.lc.guice.GuiceBasicModuleThe file inconfigure()Add the following code to the
bind(GuiceBasicService.class).to(GuiceBasicServiceImpl.class);
Copy the code

This allows us to quickly bind a service, similar to the @bean approach in Spring

Bind identifies the class we need to bind to, and to identifies the implementation class we bind to

  • I’m going to modifysrc/test/javaIn the directoryio.edurt.lc.guice.TestGuiceBasicModuleTo test the service defined by the class file, add the following code
@Test
public void test_service()
{
    Injector injector = Guice.createInjector(new GuiceBasicModule());
    GuiceBasicService service = injector.getInstance(GuiceBasicService.class);
    service.print("Hello Guice");
}
Copy the code

After running the unit test, the console prints the following:

Hello, GuiceBasicModule
print Hello Guice
Copy the code

use@ImplementedByannotations


Using the @ ImplementedBy is very simple, we only need to add interface on the interface on the class @ ImplementedBy (GuiceBasicServiceImpl. Class) annotations, the modified code is as follows

ImplementedBy tells the program that our interface is the implementation class, and Guice will automatically instantiate it for us

  • GuiceBasicService
package io.edurt.lc.guice;

@ImplementedBy(GuiceBasicServiceImpl.class)
public interface GuiceBasicService
{
    void print(String input);
}
Copy the code

The contents of the GuiceBasicServiceImpl class remain unchanged

  • deletesrc/test/javaIn the directoryio.edurt.lc.guice.TestGuiceBasicModuleIn the class fileGuiceBasicService service = injector.getInstance(GuiceBasicService.class);Code snippets.

After running the unit test, the console prints the following:

Hello, GuiceBasicModule
print Hello Guice
Copy the code

The source address


GitHub