Guice快速入门

Spring框架的依赖注入是家喻户晓的,但是在实际的开发中我们想使用便捷的依赖注入功能,但是又不想引入Spring框架的笨重和复杂性,该怎么办呢?

有了Google Guice,这个问题便简单了,首先在你的maven项目里引入

<dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    <version>4.0</version>
</dependency>

官方文档里给出的例子又臭又长,我不使用官方的例子,下面我们来写个最简单的HelloWorld

import javax.inject.Singleton;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;

@Singleton
class HelloPrinter {

	public void print() {
		System.out.println("Hello, World");
	}

}

@Singleton
public class Sample {

	@Inject
	private HelloPrinter printer;

	public void hello() {
		printer.print();
	}

	public static void main(String[] args) {
		Injector injector = Guice.createInjector();
		Sample sample = injector.getInstance(Sample.class);
		sample.hello();
	}
}

我们使用Guice创建了一个注射器Injector,然后从Injector拿到你想要的对象就可以了,Guice会自动装配依赖树。Guice的启动速度是很快的,在一个大型应用中,Guice装配所有的模块决不会超过1s。Guice是一个非常干净的依赖注入框架,框架除了依赖注入功能之外,没有任何其它非相关模块功能。

Guice里最常用的两个注解就是@Singleton和@Inject,Singleton表示构建的对象是单例的,Inject表示被标注的字段将使用Guice自动注入。在一般的项目中这两个注解一般可以完成90%以上的装配工作。

Guice需要实例化对象,请确保相应被实例化的对象有默认构造器。

当某个接口有多个实现时,我们使用@ImplementedBy注解在接口定义上,指定接口的具体实现类

import javax.inject.Singleton;
import com.google.inject.Guice;
import com.google.inject.ImplementedBy;
import com.google.inject.Inject;
import com.google.inject.Injector;

@ImplementedBy(SimpleHelloPrinter.class)
interface IHelloPrinter {
	void print();
}

@Singleton
class SimpleHelloPrinter implements IHelloPrinter {

	public void print() {
		System.out.println("Hello, Simple World");
	}

}

@Singleton
class ComplexHelloPrinter implements IHelloPrinter {

	public void print() {
		System.out.println("Hello, Complex World");
	}

}

@Singleton
public class Sample {

	@Inject
	private IHelloPrinter printer;

	public void hello() {
		printer.print();
	}

	public static void main(String[] args) {
		Injector injector = Guice.createInjector();
		Sample sample = injector.getInstance(Sample.class);
		sample.hello();
	}
}

如果我们不用Singleton标注,每次获取实例时,Guice会重新构造一个,这个会有反射构造器的性能损耗,在高性能场景下,请谨慎。

import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;

class HelloPrinter {

	private static int counter;

	private int myCounter;

	public HelloPrinter() {
		myCounter = counter++;
	}

	public void print() {
		System.out.printf("Hello, World %d\n", myCounter);
	}

}

public class Sample {

	@Inject
	private HelloPrinter printer;

	public void hello() {
		printer.print();
	}

	public static void main(String[] args) {
		Injector injector = Guice.createInjector();
		Sample sample = injector.getInstance(Sample.class);
		sample.hello();
		sample = injector.getInstance(Sample.class);
		sample.hello();
		sample = injector.getInstance(Sample.class);
		sample.hello();
		sample = injector.getInstance(Sample.class);
		sample.hello();
	}
}

我们可以不使用@ImplementedBy注解,因为这样不优雅,谁会在定义接口的时候就能预知实现类的名称呢。我们可以使用Guice Module定义装配规则,它相当于Spring的XML文件,只不过它的装配规则都是使用代码定义的。你可能会辩解说代码定义怎么能比得上XML定义呢,其实Guice Module在一个大型项目中也是非常的简洁,一般只会占用几十行代码,Module里面配置的仅仅是特殊的专配规则。就规则的可读性而言,代码要比XML舒服的多。

import javax.inject.Singleton;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;

interface IHelloPrinter {
	void print();
}

@Singleton
class SimpleHelloPrinter implements IHelloPrinter {

	public void print() {
		System.out.println("Hello, Simple World");
	}

}

@Singleton
class ComplexHelloPrinter implements IHelloPrinter {

	public void print() {
		System.out.println("Hello, Complex World");
	}

}

class SampleModule extends AbstractModule {

	@Override
	protected void configure() {
		bind(IHelloPrinter.class).to(SimpleHelloPrinter.class);
	}

}

@Singleton
public class Sample {

	@Inject
	private IHelloPrinter printer;

	public void hello() {
		printer.print();
	}

	public static void main(String[] args) {
		Injector injector = Guice.createInjector(new SampleModule());
		Sample sample = injector.getInstance(Sample.class);
		sample.hello();
	}

}

我们还可以使用@Named名称指令来指定依赖注入实现

import javax.inject.Named;
import javax.inject.Singleton;
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.Names;

interface IHelloPrinter {
	void print();
}

@Singleton
class SimpleHelloPrinter implements IHelloPrinter {

	public void print() {
		System.out.println("Hello, Simple World");
	}

}

@Singleton
class ComplexHelloPrinter implements IHelloPrinter {

	public void print() {
		System.out.println("Hello, Complex World");
	}

}

class SampleModule extends AbstractModule {

	@Override
	protected void configure() {
		bind(IHelloPrinter.class).annotatedWith(Names.named("simple")).to(SimpleHelloPrinter.class);
		bind(IHelloPrinter.class).annotatedWith(Names.named("complex")).to(ComplexHelloPrinter.class);
	}
}

@Singleton
public class Sample {

	@Inject
	@Named("simple")
	private IHelloPrinter simplePrinter;

	@Inject
	@Named("complex")
	private IHelloPrinter complexPrinter;

	public void hello() {
		simplePrinter.print();
		complexPrinter.print();
	}

	public static void main(String[] args) {
		Injector injector = Guice.createInjector(new SampleModule());
		Sample sample = injector.getInstance(Sample.class);
		sample.hello();
	}

}

我们不使用字段注入,改用构造器注入,如果我们需要在构造器里做一些特别的初始化工作

import javax.inject.Named;
import javax.inject.Singleton;
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.Names;

interface IHelloPrinter {
	void print();
}

@Singleton
class SimpleHelloPrinter implements IHelloPrinter {

	public void print() {
		System.out.println("Hello, Simple World");
	}

}

@Singleton
class ComplexHelloPrinter implements IHelloPrinter {

	public void print() {
		System.out.println("Hello, Complex World");
	}

}

class SampleModule extends AbstractModule {

	@Override
	protected void configure() {
		bind(IHelloPrinter.class).annotatedWith(Names.named("simple")).to(SimpleHelloPrinter.class);
		bind(IHelloPrinter.class).annotatedWith(Names.named("complex")).to(ComplexHelloPrinter.class);
	}

}

@Singleton
public class Sample {

	@Named("simple")
	private IHelloPrinter simplePrinter;

	@Named("complex")
	private IHelloPrinter complexPrinter;

	@Inject
	public Sample(@Named("simple") IHelloPrinter simplePrinter, @Named("complex") IHelloPrinter complexPrinter) {
		this.simplePrinter = simplePrinter;
		this.complexPrinter = complexPrinter;
	}

	public void hello() {
		simplePrinter.print();
		complexPrinter.print();
	}

	public static void main(String[] args) {
		Injector injector = Guice.createInjector(new SampleModule());
		Sample sample = injector.getInstance(Sample.class);
		sample.hello();
	}

}

还可以自动注入Set,Map容器,但是得首先加上扩展库

<dependency>
    <groupId>com.google.inject.extensions</groupId>
    <artifactId>guice-multibindings</artifactId>
    <version>4.0</version>
</dependency>

注入Set

import java.util.Set;
import javax.inject.Inject;
import javax.inject.Singleton;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.multibindings.Multibinder;

interface IHelloPrinter {
	void print();
}

@Singleton
class SimpleHelloPrinter implements IHelloPrinter {

	public void print() {
		System.out.println("Hello, Simple World");
	}

}

@Singleton
class ComplexHelloPrinter implements IHelloPrinter {

	public void print() {
		System.out.println("Hello, Complex World");
	}

}

class SampleModule extends AbstractModule {

	@Override
	protected void configure() {
		Multibinder<IHelloPrinter> printers = Multibinder.newSetBinder(binder(), IHelloPrinter.class);
		printers.addBinding().to(SimpleHelloPrinter.class);
		printers.addBinding().to(ComplexHelloPrinter.class);
	}

}

@Singleton
public class Sample {

	@Inject
	private Set<IHelloPrinter> printers;

	public void hello() {
		for (IHelloPrinter printer : printers) {
			printer.print();
		}
	}

	public static void main(String[] args) {
		Injector injector = Guice.createInjector(new SampleModule());
		Sample sample = injector.getInstance(Sample.class);
		sample.hello();
	}

}

注入Map

import java.util.Map;
import javax.inject.Inject;
import javax.inject.Singleton;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.multibindings.MapBinder;

interface IHelloPrinter {
	void print();
}

@Singleton
class SimpleHelloPrinter implements IHelloPrinter {

	public void print() {
		System.out.println("Hello, Simple World");
	}

}

@Singleton
class ComplexHelloPrinter implements IHelloPrinter {

	public void print() {
		System.out.println("Hello, Complex World");
	}

}

class SampleModule extends AbstractModule {

	@Override
	protected void configure() {
		MapBinder<String, IHelloPrinter> printers = MapBinder.newMapBinder(binder(), String.class, IHelloPrinter.class);
		printers.addBinding("simple").to(SimpleHelloPrinter.class);
		printers.addBinding("complex").to(ComplexHelloPrinter.class);
	}

}

@Singleton
public class Sample {

	@Inject
	private Map<String, IHelloPrinter> printers;

	public void hello() {
		for (String name : printers.keySet()) {
			printers.get(name).print();
		}
	}

	public static void main(String[] args) {
		Injector injector = Guice.createInjector(new SampleModule());
		Sample sample = injector.getInstance(Sample.class);
		sample.hello();
	}
}

在全世界都沉迷于复杂的Spring框架时,Guice无疑是一股清流,在炎热的夏天,它就像一杯冰爽的橙汁,让人畅快不已。

以上内容还不够看,请移步官方文档https://github.com/google/guice/wiki/GettingStarted

原文发布于微信公众号 - 码洞(codehole)

原文发表时间:2017-12-24

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Jerry的SAP技术分享

使用Java connector消费ABAP系统的函数

Java Connector(JCO)环境的搭建:Step by step to download and configure JCO in your lapt...

1902
来自专栏分布式系统进阶

Librdkafka的基础数据结构 4 --- String和ByteArray

1403
来自专栏CodingToDie

个人开源项目 FastSQL

一个简单的数据库工具类,可以简化 DB 操作,减少 SQL 语句的书写,同时提供将 SQL 转换 Bean 和将 Bean转换 SQL 的方法,

1322
来自专栏calmound

struts2+Hibernate实现用户登陆功能

实现的功能,在登陆页面输入Username和PassWord后,将username和password通过Hibernate匹对数据库是否含有一样的usernam...

3469
来自专栏cmazxiaoma的架构师之路

使用Hibernate、JPA、Lombok遇到的有趣问题

3204
来自专栏Java编程技术

SpringBoot+MyBatis通用Mapper3实践浅析

MyBatis通用Mapper3是对mybaits的数据库操作的一层封装,之前使用时候是根据数据库表生成mapper.xml,mapper的接口类,这个mapp...

1571
来自专栏醉生梦死

java抓取豆瓣电影数据,分析电影评分,生成统计图表 ---servlet

6234
来自专栏Java成神之路

HQL语句大全

Hibernate配备了一种非常强大的查询语言,这种语言看上去很像SQL。但是不要被语法结构 上的相似所迷惑,HQL是非常有意识的被设计为完全面向对象的查询,它...

1795
来自专栏LhWorld哥陪你聊算法

Hadoop源码篇---解读Mapprer源码outPut输出

上次讲完MapReduce的输入后,这次开始讲MapReduce的输出。注意MapReduce的原语很重要:

1363
来自专栏别先生

Java枚举类使用和总结

1142

扫码关注云+社区

领取腾讯云代金券