跳转至

Guice依赖注入(Scope其他方式)

本文章主要详细讲解Guice依赖注入中的一些高级选项,他们分别是Eagerly Loading BindingsStageOptional Injection。我们将一一对他们进行讲解。

基础环境


技术 版本
Java 1.8+
Guice 4.2.3

初始化项目


  • 初始化项目
mvn archetype:generate -DgroupId=io.edurt.lc.guice -DartifactId=guice-binder-scope-other -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0 -DinteractiveMode=false
  • 修改pom.xml增加Guice依赖
<?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">
    <parent>
        <artifactId>lc-guice</artifactId>
        <groupId>io.edurt.lc.guice</groupId>
        <version>1.0.0</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>guice-binder-scope-other</artifactId>
    <name>Learning Center for Guice Binder(Scope Other)</name>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.inject</groupId>
            <artifactId>guice</artifactId>
            <version>4.2.3</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

guice: guice就是我们核心要使用的依赖

Eagerly Loading Bindings


除绑定Scope外,对象默认在第一次调用时被创建,也就是Eagerly Loading Bindings延时加载,Guice也允许对象在注入到Guice容器中时就被创建出来(只是针对单例模式才有效)。

  • src/main/java目录下新建 io.edurt.lc.guice.GuiceEagerlyLoadingBindingsService 类文件,在文件输入以下内容
package io.edurt.lc.guice;
public class GuiceEagerlyLoadingBindingsService
{
    public GuiceEagerlyLoadingBindingsService()
    {
        System.out.println("Init Success");
    }
    public void println()
    {
        System.out.println("After Eagerly Loading Bindings");
    }
}
  • 接下来在src/test/java目录创建 io.edurt.lc.guice.TestGuiceEagerlyLoadingBindings 类文件进行定义的服务进行测试,添加以下代码
package io.edurt.lc.guice;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.junit.Test;
public class TestGuiceEagerlyLoadingBindings
{
    @Test
    public void test()
    {
        Injector injector = Guice.createInjector(binder -> binder.bind(GuiceEagerlyLoadingBindingsService.class).asEagerSingleton());
        injector.getInstance(GuiceEagerlyLoadingBindingsService.class).println();
    }
}

我们运行程序输出

Init Success
After Eagerly Loading Bindings

通过运行结果我们可以看到在getInstance获取实例时,该实例就被构建出来.

Stages


Guice中有默认三种运行方式来控制Guice容器的加载速度.在com.google.inject.Stage枚举中提供了TOOLDEVELOPMENTPRODUCTION三种模式。

默认源码中使用的是DEVELOPMENT方式,源码如下

public static Injector createInjector(Iterable<?extends Module> modules){
        return createInjector(Stage.DEVELOPMENT,modules);
        }

如需指定自己的格式,代码如下

Injector injector=Guice.createInjector(Stage.DEVELOPMENT,binder->binder.bind(GuiceEagerlyLoadingBindingsService.class).asEagerSingleton());

在构建时指定Stage即可.

Optional Injection


Optional Injection可以用于我们选择性的注入内容, 也就是添加一个默认对象.

  • src/main/java目录下新建 io.edurt.lc.guice.GuiceService 类文件,在文件输入以下内容
package io.edurt.lc.guice;
public interface GuiceService
{
    void println();
}
  • src/main/java目录下新建 io.edurt.lc.guice.GuiceOptionalInjectionDefaultService 类文件,在文件输入以下内容
package io.edurt.lc.guice;
public class GuiceOptionalInjectionDefaultService
        implements GuiceService
{
    @Override
    public void println()
    {
        System.out.println("Optional Injection Default");
    }
}
  • src/main/java目录下新建 io.edurt.lc.guice.GuiceOptionalInjectionDefaultService 类文件,在文件输入以下内容
package io.edurt.lc.guice;
public class GuiceOptionalInjectionCustomService
        implements GuiceService
{
    @Override
    public void println()
    {
        System.out.println("Optional Injection Custom");
    }
}
  • 接下来在src/test/java目录创建 io.edurt.lc.guice.TestGuiceOptionalInjection 类文件进行定义的服务进行测试,添加以下代码
package io.edurt.lc.guice;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import org.junit.Test;
public class TestGuiceOptionalInjection
{
    @Inject(optional = true)
    private GuiceService service = new GuiceOptionalInjectionDefaultService();
    @Test
    public void testDefault()
    {
        Injector injector = Guice.createInjector(binder -> {
        });
        injector.getInstance(TestGuiceOptionalInjection.class).service.println();
    }
    @Test
    public void testCustom()
    {
        Injector injector = Guice.createInjector(binder -> {
            binder.bind(GuiceService.class).to(GuiceOptionalInjectionCustomService.class);
        });
        injector.getInstance(TestGuiceOptionalInjection.class).service.println();
    }
}

当我们运行testDefault()时,控制台输出

Optional Injection Default

当我们运行testCustom()时,控制台输出

Optional Injection Custom

Guice容器中无法获取获取一个GuiceService服务那么就使用默认的GuiceOptionalInjectionDefaultService,否则就是用获取到的GuiceOptionalInjectionCustomService服务。

源码地址


GitHub

回到页面顶部