dagger2 备注
dagger 2是android下的IOC框架,类似java服务端的spring,但功能上远没有其强大。个人理解不管是APP还是服务端依赖注入的本质都是一样的,用于解耦某个服务的定义和实现。我自己给出下面这个简单的例子:
1、在android studio中增加配置如下:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0',
'com.neenbedankt.gradle.plugins:android-apt:1.4+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
apply plugin: 'android-apt'
//此处要注意dagger的版本号
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
compile 'com.google.dagger:dagger:2.0'
apt 'com.google.dagger:dagger-compiler:2.0'
provided 'org.glassfish:javax.annotation:10.0-b28'
}
2、定义服务的实现如下:
public interface IStoreInfo {
void storeInfo(String info);
}
public class StoreInfoInDb implements IStoreInfo{
@Override
public void storeInfo(String info) {
System.out.println("In DB:" + info);
}
}
public class StoreInfoInFile implements IStoreInfo{
@Override
public void storeInfo(String info) {
System.out.println("In File:" + info);
}
}
3、定义module和component
@Module
public class InfoServiceModule { @Provides
@Singleton
IStoreInfo setIStoreInfo(){
return new StoreInfoInFile();
}
}
@Singleton
@Component(modules = InfoServiceModule.class)
public interface InfoServiceComponent {
void inject(InfoService service);
IStoreInfo setIStoreInfo();
}
备注:这里其实相当于spring XML定义的部分,只是这里是采用硬编码的形式绑定接口的定义和实现
4、使用:
public class InfoService {
@Inject
public IStoreInfo info;
public void initService(){
InfoServiceComponent component = DaggerInfoServiceComponent.builder().infoServiceModule(new InfoServiceModule()).build();
component.inject(this);
}
public void infoHandler(String input){
info.storeInfo(input);
}
}
dagger2 备注的更多相关文章
- DBCP 配置备注
<property name="initialSize" value="5"></property> <property name ...
- JNI 备注
本文记录一个基础的JNI例子及过程中遇到的问题解决. 1.定义一个JAVA类如下: package jnidemo01; public class JniHello { public native v ...
- DoraCMS 源码知识点备注
项目需要研究了下DoraCMS这款开源CMS,真心做的不错:).用的框架是常用的express 4 + mongoose,代码也很规范,值得学习. 源码中一些涉及到的小知识点备注下: https:// ...
- Dagger2系列之使用方法
本系列只讲使用方法和使用中遇到的问题,如果还对dagger2还不了解的童鞋儿可以参考文章: http://www.jianshu.com/p/cd2c1c9f68d4 http://www.jians ...
- 如何用代码读取Office Online Server2016的文档的备注信息
前言 在一个项目上客户要求读取office online server 2016的对文档的备注信息,如下图: 以前思路老纠结在OOS这个在线上,总有以为这个信息存储在某个列表中,其实错了,这个备注信息 ...
- Using Dagger2 in Android
Dagger2是一个Java和Android的依赖注入框架. 本文介绍Android中dagger2的基本使用. 其中包括@Inject, @Component, @Module和@Provides注 ...
- Dagger2 (三) 总结篇
一.Dagger2注入原理 Dagger2以自动生成代码的形式,帮助我们构建依赖图,在使用依赖的时候方便清晰,这里说明一点,在我们使用Dagger2的时候,绝大多数错误都是编译器就会暴漏出来,这也就决 ...
- Dagger2 (二) 进阶篇
一.作用域Scope 之前了解RoboGuice的时候,我们知道它默认给我们提供了几个注解,ContextSingleton和Singleton,但是Dagger2更为灵活,只有javax包中提供的S ...
- greenDao 备注
1.构建文件夹存放自动生成的代码,并在脚本中配置: sourceSets { main { java.srcDirs = ['src/main/java', 'src/main/java-gen'] ...
随机推荐
- ArrayList源码阅读笔记(基于JDk1.8)
关键常量: private static final int DEFAULT_CAPACITY = 10; 当没有其他参数影响数组大小时的默认数组大小 private static final Obj ...
- swift 手机号码正则表达式 记录一下
func isTelNumber(num:NSString)->Bool { var mobile = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$&qu ...
- js实现下拉菜单
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Kafka replication
Kafka replication kafka_replication_detailed_design_v2.pdf kafka Detailed Replication Design V3 Apac ...
- php正则逆向引用与子模式分析
先看一个例子: <?php $string = 'April 15, 2003'; $pattern = '/(\w+) (\d+), (\d+)/i'; $replacement = '${1 ...
- ACM :漫漫上学路 -DP -水题
CSU 1772 漫漫上学路 Time Limit: 1000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu Submit ...
- js排序算法总结——冒泡,快速,选择,插入,希尔,归并
相信排序是任何一个程序猿都会用到的东西,今天简单总结记录下常见的排序算法. 一.冒泡排序 说起冒泡排序,可能每个人都不会陌生,实现思路相当简单明了,就是不停的对数组进行两两比较,将较大(较小)的一项放 ...
- 彻底理解webservice SOAP WSDL
WebServices简介 先给出一个概念 SOA ,即Service Oriented Architecture ,中文一般理解为面向服务的架构, 既然说是一种架构的话,所以一般认为 SOA 是包含 ...
- Angular初步
一.angular.js是什么? angular.js是一个javascript的框架,与jquery是一个级别的,区别是jquery主要是擅长dom操作,而angular主要是擅长绑定数据显示数据. ...
- lua 时间戳和时间互转
1.时间戳转换成时间 local t = 1412753621000 function getTimeStamp(t) return os.date("%Y%m%d%H", ...