当前位置:网站首页>Google开源依赖注入框架-Guice指南
Google开源依赖注入框架-Guice指南
2022-06-28 09:41:00 【软件质量保障】
持续坚持原创输出,点击蓝字关注我吧
作者:软件质量保障
知乎:https://www.zhihu.com/people/iloverain1024
之前发过一篇文章《浅谈依赖注入的实现》,介绍了依赖注入的实现原理。文中提到高效实现依赖注入的工具Guice,本文就介绍一下这款Google开源的依赖注入框架Guice及其使用方法。
1. 简介
Google Guice 是一个轻量级的依赖注入框架,它支持Java 5或者更高版本的JDK,得利于Java 5中提供的泛型 (Generics) 和注解 (Annotations) ,它可以使得代码类型安全 (type-safe) 。那么何时使用在代码中使用 Guice 进行注入呢?一般来说,如果在你的应用代码中业务对象 (Business Objects) 之间的关系或者依赖需要维护的话,你就可以使用Guice 进行注入。
本文会通过一些例子来初步的认识一下 Guice 框架。
当然,不了解依赖注入基础知识的同学建议先看下这篇科普贴 《浅谈依赖注入的实现》。
2. 添加依赖
将以下依赖项添加到Maven项目中放入pom.xml中:
<dependency><groupId>com.google.inject</groupId><artifactId>guice</artifactId><version>4.1.0</version></dependency>
3. Guice的基础用法
3.1 项目样例代码
我还是用上篇文章中那个例子作为演示场景,即以现实生活中的三种通信方式为例:Email、SMS 和 IM。
首先,我们定义Communication类:
public class Communication {@Injectprivate Logger logger;@Injectprivate Communicator communicator;public Communication(Boolean keepRecords) {if (keepRecords) {System.out.println("Message logging enabled");}}public boolean sendMessage(String message) {return communicator.sendMessage(message);}}
这个Communication类是通信基类,此类的实例实现了通过可用的通信通道发送消息。如上代码所示,Communication会依赖Communicator,我们通过调用它来进行实际的消息传输。
Guice最基础的用法就是通过Injector对象实现,下面是客户端代码的例子:
public static void main(String[] args){Injector injector = Guice.createInjector();Communication comms = injector.getInstance(Communication.class)comms.sendMessage("软件质量保障");}
Guice 对依赖注入和管理采用代码优先的策略,因此我们可以不用处理很多令人抓狂的XML配置。
3.2. Guice bind
Binding is to Guice as wiring is to Spring。通过bind,我们可以实现Guice如何将依赖项注入到一个类中,
我们在com.google.inject.AbstractModule的实现中定义:
public class BasicModule extends AbstractModule {@Overrideprotected void configure() {bind(Communicator.class).to(DefaultCommunicatorImpl.class);}}
此模块实现将Communicator绑定到其默认实现类DefaultCommunicatorImpl上,在找到Communicator的地方都将注入Default CommunicatorImpl的实例。
3.3. @Named注解
我们可以使用 @Named注解来命名这些实体类,当你给它一个命名,它会返回一个命名好的 Annotation。例如在上面的例子中,可以使用 Names.named() 来完成相同的事情。
@Inject @Named("DefaultCommunicator")Communicator communicator;
@Overrideprotected void configure() {bind(Communicator.class).annotatedWith(Names.named("DefaultCommunicator")).to(DefaultCommunicatorImpl.class);}
使用@Named(“DefaultCommunicator”)注解将Communicator绑定到DefaultCommunicator实现类。
3.4. 构造函数绑定
我们还可以使用构造函数绑定注入一个没有默认无参数构造函数的依赖对象:
public class BasicModule extends AbstractModule {
@Override protected void configure() {
bind(Boolean.class).toInstance(true); bind(Communication.class).toConstructor( Communication.class.getConstructor(Boolean.TYPE));}构造函数绑定的另一种方法是实例绑定,我们直接为Communication.class绑定一个实例:
public class BasicModule extends AbstractModule {@Overrideprotected void configure() {bind(Communication.class).toInstance(new Communication(true));}}
无论我们在何处声明Communication类,此绑定都将提供Communication类的实例。但是在这种情况下,类的依赖关系树不会自动关联。
4. 依赖注入类型
Guice支持DI所推荐的标准注入类型。假设在Communicator类中,我们需要注入不同类型的CommunicationMode,可以通过下面几种方法实现。
4.1 属性注入
@Inject @Named("SMSComms")CommunicationMode smsComms;
我们可以使用@Named注解作为限定符来实现基于名称的定向注入。
4.2. Method注入
这里我们将使用一个setter方法来实现注入:
@Injectpublic void setEmailCommunicator(@Named("EmailComms") CommunicationMode emailComms) {this.emailComms = emailComms;}
4.3. 构造函数注入
我们还可以使用构造函数注入依赖:
@Injectpublic Communication(@Named("IMComms") CommunicationMode imComms) {this.imComms= imComms;}
4.4. 隐式注入
Guice 还提供隐式注入一些通用组件,例如Injector和java.util.Logger的实例等。大家是不是发现了,我们的所有示例都使用了Logger,但你是不是找不到它的实际绑定代码。
5. Guice Scope机制
Guice支持我们在其他DI框架中逐渐习惯的Scope和Scope机制。
5.1 单例
下面在我们的应用程序中注入一个单例,我们指定了Communicator的Scope,它将会被标志为一个单例实例。
bind(Communicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(Communicator.class).in(Scopes.SINGLETON);
5.2. 饿汉式单例
下面注入一个饿汉式单例,asEagerSingleton()方法用来标记单例模式。
bind(Communicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(Communicator.class).asEagerSingleton();
喜欢的话,就点个赞和在看再走吧
- END -
下方扫码关注 软件质量保障,与质量君一起学习成长、共同进步,做一个职场最贵Tester!
后台回复【测开】获取测试开发xmind脑图
后台回复【加群】获取加入测试社群!
往期推荐
边栏推荐
- 老板叫我写个APP自动化--Yaml文件读取--内附整个框架源码
- Bridge mode
- PMP考试重点总结五——执行过程组
- 【OpenCV 例程200篇】213. 绘制圆形
- Explain final, finally, and finalize
- 读取pdf文字和excel写入操作
- PMP考试重点总结八——监控过程组(2)
- Inventory of excellent note taking software: good-looking and powerful visual note taking software, knowledge map tools heptabase, hydrogen map, walling, reflect, infranodus, tiddlywiki
- 01 distributed system overview
- 我大抵是卷上瘾了,横竖睡不着!竟让一个Bug,搞我两次!
猜你喜欢

Virtual machine 14 installing win7 (Figure tutorial)

虚拟机14安装win7(图教程)

组合模式(Composite Pattern)

纵观jBPM从jBPM3到jBPM5以及Activiti

How to view the web password saved by Google browser

The digital human industry is about to break out. What is the market pattern?

Static page of pinyougou mall

Dolphin scheduler uses system time

如图 用sql行转列 图一原表,图二希望转换后
![[happy Lantern Festival] guessing lantern riddles eating lantern festival full of vitality ~ (with lantern riddle guessing games)](/img/04/454bede0944f56ba69cddf6b237392.jpg)
[happy Lantern Festival] guessing lantern riddles eating lantern festival full of vitality ~ (with lantern riddle guessing games)
随机推荐
PMP考试重点总结七——监控过程组(1)
2020-10-27
装饰模式(Decorator)
Django database operation and problem solving
Matplotlib attribute and annotation
读取pdf文字和excel写入操作
Comprehensive evaluation of outline note taking software workflow: advantages, disadvantages and evaluation
再见!IE浏览器,这条路由Edge替IE继续走下去
Summary of MySQL basic knowledge points
Dbeaver connects to kingbasees V8 (ultra detailed graphic tutorial)
SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
RESTful风格
Instant messaging and BS architecture simulation of TCP practical cases
全局异常处理器与统一返回结果
虛擬機14安裝win7(圖教程)
Bron filter Course Research Report
Data visualization makes correlation analysis easier to use
Huawei OSPF single region
Composite pattern
栈的弹出压入序列<难度系数>