使用Dagger2做静态注入, 对比Guice.
Dagger
依赖注入的诉求, 这边就不重复描述了, 在上文Spring以及Guice的IOC文档中都有提及, 既然有了Guice,
Google为啥还要搞个Dagger2出来重复造轮子呢? 因为使用动态注入, 虽然写法简单了, 耦合也降低了,
但是带来了调试不方便, 反射性能差等一些缺点.
而Dagger跟Guice最大的差异在于, 他是编译期注入的, 而不是运行时.
他生成的代码可以直观的调试, 也不是通过反射, 而是通过构建工厂类. 下面我们用代码来简单演示一下.
构建工程
既然Dagger是静态注入的, 那么他自然也跟其他动态注入框架工程有点区别,
编译时需要额外依赖dagger-compiler, dagger-producers等,

不过运行时的jar只需要dagger以及javax.inject包即可.
好在Google为我们提供了pom文件, 我们只需要在idea里新建maven工程, 在pom文件中导入如下内容, 他会自动下载依赖.
<?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">
<modelVersion>4.0.0</modelVersion> <groupId>com.maven.dagger2</groupId>
<artifactId>com.maven.dagger2</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>com.google.dagger</groupId>
<artifactId>dagger</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.google.dagger</groupId>
<artifactId>dagger-compiler</artifactId>
<version>2.2</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
第一个注入程序
我们以一个打印系统为例, 打印业务类PrintJob, 里面有一份报表Reportpage待打印.
public class ReportPage{
public void print(){
System.out.println("开始打印报表");
}
}
public class PrintJob {
2 // 需要打印的报表
public ReportPage reportPage;
public void setReportPage(ReportPage reportPage) {
this.reportPage = reportPage;
}
public void print() {
this.reportPage.print();
}
public static void main(String[] args) throws InterruptedException {
// 初始化报表
ReportPage page = new ReportPage();
PrintJob job = new PrintJob();
job.setReportPage(page);
//执行打印
job.print();
}
}
在main函数中, 我们初始化了Printjob以及它里面的报表对象, 并执行打印.
下面我们通过Dagger注入的方式来写.
写法很简单, 跟Guice类似, 我们只需要在reportpage成员上加@Inject注解.
同时添加一个Component对象, 用来告诉Dagger, 应该注入到该类, 并扫描其中@Inject的成员
@Component
public interface PrintjobComponent { void inject(PrintJob job);
}
添加完Component以及@Inject注解后我们需要编译代码或者rebuild工程, 让Dagger为我们生成工厂类.
生成的代码位于target/generated-sources目录. 里面会有一个叫DaggerPrintjobComponent的类.

idea会自动将当期路径标记成Classpath, 因此我们也不需要把他手动拷贝出来.
如果没有自动import, 可以右键pom.xml->Maven ->Reimport.
我们在Printjob的构造函数里加上DaggerPrintjobComponent.create().inject(this);来实现注入
public class PrintJob {
@Inject
public ReportPage reportPage;
public PrintJob() {
DaggerPrintjobComponent.create().inject(this);
}
public void print() {
this.reportPage.print();
}
public static void main(String[] args) throws InterruptedException {
// 看上去清爽了一点
PrintJob job = new PrintJob();
job.print();
}
}
public class ReportPage {
@Inject
public ReportPage() {
System.out.println("初始化成功!!!");
}
public void print(){
System.out.println("开始打印报表");
}
}
相比于一开始的非注入写法, 在外部是看不到赋值操作的.
有人会说, 那我直接在printjob的构造函数里new reportpage()不就行了, 为什么要这么费事呢.
原因很简单, 大型系统里, printjob只存在一个接口, 他无法, 也不需要直接new reportpage()对象.

下面演示如何注入接口对象.
注入接口对象
我们给reportpage增加一个接口, 并在printjob中修改为接口声明.
public class ReportPage implements ReportPageProvider{
public interface ReportPageProvider {
void print();
}
public class PrintJob {
@Inject
public ReportPageProvider reportPage;
这个时候会发现, 运行注入报错了, 原因很简单, 我们@inject依然加载reportpage对象上,
此时他是一个接口, 接口是无法直接被实例化的.
因此我们需要引入Module对象来处理接口, 其实就是类似于一个工厂提供类.
@Module
public class ReportPageModule { @Provides
public ReportPageProvider createPage() {
return new ReportPage();
}
}
然后在component中引入module, 其他代码不用改, 依然直接new printjob().print()对象.
@Component(modules = ReportPageModule.class)
public interface PrintjobComponent { void inject(PrintJob job);
}
接口存在多个实现
我们给ReportpageProvider再增加一个子类NewReportPage, 修改Module, 增加一个方法, 构造NewReportPage.
@Module
public class ReportPageModule { @Provides
public ReportPageProvider createPage() {
return new ReportPage();
} @Provides
public ReportPageProvider createNewReportPage() {
return new NewReportPage();
} }
这个时候直接编译是无法通过的, 相同返回类型的provider只能添加一个, 如果添加多个, dagger将报错, 存在多个提供类.

此时我们就要跟Guice里一样, 使用@Named注解来标识了
@Named("new")
public ReportPageProvider reportPage;
调用的时候也很简单
@Inject
@Named("new")
public ReportPageProvider reportPage;
同理, 也可以通过@Qualifier来自定义注解标识.
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
public @interface NewReportMark {}
然后在调用的地方加上 @NewReportMark即可.

Scope生命周期
默认对象都是每次都new的, 如果想要单例实现, 则需要添加@Singleton.
在Component以及Module都加上Singleton注解.
@Singleton
@Component(modules = ReportPageModule.class)
public interface PrintjobComponent { void inject(PrintJob job);
}
@Provides
@Named("new")
@Singleton
public ReportPageProvider createNewReportPage() {
return new NewReportPage();
}
我们给Printjob中再增加一个reportpage对象, 并打印他们的hashcode.
@Inject
@Named("new")
public ReportPageProvider reportPage; @Inject
@Named("new")
public ReportPageProvider reportPage2; ...... PrintJob job = new PrintJob();
System.out.println(job.reportPage);
System.out.println(job.reportPage2);
加上Singleton注解后, 打印出来的hashcode是一致的了.
但是, 如果我们再new 一个Printjob, 打印他的reportpage.
PrintJob job = new PrintJob();
System.out.println(job.reportPage);
System.out.println(job.reportPage2); PrintJob job2 = new PrintJob();
System.out.println(job2.reportPage);
System.out.println(job2.reportPage2);
会发现前两个的hashcode跟后两个的不一样, 这就很蛋疼了. 他只是一个作用于当前component的伪单例.
那么如何实现真单例呢, 其实就是想办法把Component搞成单例的.
这样他里面的对象也都是同一个作用域下的单例了.
我们添加一个SingletonPrintjobComponent, 写法与PrintjobComponent一致.
编译后生成DaggerSingletonPrintjobComponent. 然后修改printjob构造函数中的注入.
DaggerPrintjobComponent.create().inject(this); 改成如下:
public class PrintJob {
private static SingletonPrintjobComponent component = DaggerSingletonPrintjobComponent.create();
@Inject
@Named("new")
public ReportPageProvider reportPage;
@Inject
@Named("new")
public ReportPageProvider reportPage2;
public PrintJob() {
component.inject(this);
}
public void print() {
this.reportPage.print();
}
public static void main(String[] args) throws InterruptedException {
PrintJob job = new PrintJob();
System.out.println(job.reportPage);
System.out.println(job.reportPage2);
PrintJob job2 = new PrintJob();
System.out.println(job2.reportPage);
System.out.println(job2.reportPage2);
}
}
这样的话, 多个printjob打印出来的reportpage就是一致的了, 因为都是位于同一个static的component中.

Lazy 延迟初始化
默认对象是inject的时候初始化, 如果使用Lazy封装一下, 则可以在get的时候再初始化.
@Inject
@Named("old")
public Lazy<ReportPageProvider> oldReportPage;
PrintJob job = new PrintJob();
Thread.sleep(3000);
// 对象会在get()方法调用的时候触发初始化
job.oldReportPage.get().print();
到这边就结束了, 可以看到Dagger使用上跟Guice基本差不多, 各个注解概念也类似,
最大的区别就是非动态注入, 非反射实现, 而是编译期静态注入.
使用Dagger2做静态注入, 对比Guice.的更多相关文章
- Android项目使用Dagger2进行依赖注入
原文链接:http://code.tutsplus.com/tutorials/dependency-injection-with-dagger-2-on-android–cms-23345 依赖注入 ...
- Spring静态注入的三种方式
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/chen1403876161/article/details/53644024Spring静态注入的三 ...
- spring 静态注入
1.静态注入 在setter 方法修改为非 static , 然后在上面注入即可 @Component public class WeixinConfig { // token public stat ...
- 日志系统实战(一)—AOP静态注入
背景 近期在写日志系统,需要在运行时在函数内注入日志记录,并附带函数信息,这时就想到用Aop注入的方式. AOP分动态注入和静态注入两种注入的方式. 动态注入方式 利用Remoting的Context ...
- 基于Mono.Cecil的静态注入
Aop注入有2种方式:动态注入和静态注入,其中动态注入有很多实现了 动态注入有几种方式: 利用Remoting的ContextBoundObject或MarshalByRefObject. 动态代理( ...
- Android 使用dagger2进行依赖注入(基础篇)
0. 前言 Dagger2是首个使用生成代码实现完整依赖注入的框架,极大减少了使用者的编码负担,本文主要介绍如何使用dagger2进行依赖注入.如果你不还不了解依赖注入,请看这一篇. 1. 简单的依赖 ...
- 用keras做SQL注入攻击的判断
本文是通过深度学习框架keras来做SQL注入特征识别, 不过虽然用了keras,但是大部分还是普通的神经网络,只是外加了一些规则化.dropout层(随着深度学习出现的层). 基本思路就是喂入一堆数 ...
- spring静态注入
与其说是静态注入(IOC),不如讲是对JavaBean 的静态成员变量进行赋值. 一般我们在使用依赖注入的时候,如果当前对象(javaBean )创建(实例化)一次,那么非静态的成员变量也会实例化一次 ...
- 转: spring静态注入
与其说是静态注入(IOC),不如讲是对JavaBean 的静态成员变量进行赋值. 一般我们在使用依赖注入的时候,如果当前对象(javaBean )创建(实例化)一次,那么非静态的成员变量也会实例化一次 ...
随机推荐
- Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution
B. Arpa’s obvious problem and Mehrdad’s terrible solution time limit per test 1 second memory limit ...
- 关于C++中vector和set使用sort方法进行排序
C++中vector和set都是非常方便的容器, sort方法是algorithm头文件里的一个标准函数,能进行高效的排序,默认是按元素从小到大排序 将sort方法用到vector和set中能实现多种 ...
- Python-数据类型-转摘
1.数字 2 是一个整数的例子.长整数 不过是大一些的整数.3.23和52.3E-4是浮点数的例子.E标记表示10的幂.在这里,52.3E-4表示52.3 * 10-4.(-5+4j)和(2.3-4. ...
- android 横竖屏切换不重走生命周期
android在系统配置发生改变时,Activity会被重新创建,但是某些情况下我们希望系统配置改变时不会重新创建Activity,这个时候我们可以给Activity指定相对应的configChang ...
- 基于webpack搭建的vue+element-ui框架
花了1天多的时间, 终于把这个框架搭建起来了. 好了, 不多说了, 直接进入主题了.前提是安装了nodejs,至于怎么安装, 网上都有教程. 这里就不多说了, 这边使用的IDE是idea.1.在E:/ ...
- SQL Server 行转列,列转行。多行转成一列
一.多行转成一列(并以","隔开) 表名:A 表数据: 想要的查询结果: 查询语句: SELECT name , value = ( STUFF(( SELECT ',' + va ...
- selenium页面元素操作(简易版)
介绍一下,这是处理页面元素的基本方法,@selenium 发送文字 element.send_keys(keys_to_send) 单击 element.click() 提交表单 el ...
- 【2】构建一个SSM项目结构
初步思考一下这个项目的结构,由于是给一个比较老的公司做这个外包项目,服务器是搭建在windows操作系统上的Tomcat6.0,系统的JDK版本也是JDK1.6,都是比较旧. 数据库方面有专人负责,所 ...
- Java8一:Lambda表达式教程
1. 什么是λ表达式 λ表达式本质上是一个匿名方法.让我们来看下面这个例子: public int add(int x, int y) { return x + y; } 转成 ...
- C#Session丢失问题的解决办法
关于c# SESSION丢失问题解决办法 我们在用C#开发程序的时候经常会遇到Session很不稳定,老是数据丢失.下面就是Session数据丢失的解决办法希望对您有好处.1.在WEB.CONFI ...