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'] ...
随机推荐
- C语言的可变参数在Linux(Ubuntu)与Windows下注意点
基本上C语言的可变参数原理在不同平台和不同编译器下基本类似(通过函数入栈,从右向左,从高位到低位地址),不过部分实现会有所不同:在使用中需要注意的是: va_list 为char 类型指针,部分调用如 ...
- CozyRSS1.0 - 有可用性版本
bin:还是不提供了,有兴趣的加QQ群373862115讨论 src:https://github.com/zpublic/cozy/tree/a202ae0944936c1ca56c2c3f4b73 ...
- 题目:求1+2+…+n,
题目:求1+2+-+n, 要求不能使用乘除法.for.while.if.else.switch.case等关键字 以及条件判断语句(A?B:C). java 实现 public class sum { ...
- node - glob模块
node的glob模块允许你使用 *等符号, 来写一个glob规则,像在shell里一样,获取匹配对应规则的文件. 这个glob工具基于javascript.它使用了 minimatch 库来进行匹配 ...
- STL插入删除和查询测试
博客园的markdown不知道怎么插入C++代码,只好放弃了..本文在我的blog发布:http://jwk000.github.io/2015/09/02/20150902/
- [spring源码学习]八、IOC源码-messageSource
一.代码实例 我们在第八章可以看到,spring的context在初始化的时候,会默认调用系统中的各种约定好的bean,其中第一个bean就是id为messageSource的bean,我们了解这应该 ...
- LeetCode 1. Two Sum
Problem: Given an array of integers, return indices of the two numbers such that they add up to a sp ...
- 【oracle】union、union all、intersect、minus 的用法及区别
一.union与union all 首先建两个view create or replace view test_view_1 as as c from dual union as c from dua ...
- [深度学习大讲堂]从NNVM看2016年深度学习框架发展趋势
本文为微信公众号[深度学习大讲堂]特约稿,转载请注明出处 虚拟框架杀入 从发现问题到解决问题 半年前的这时候,暑假,我在SIAT MMLAB实习. 看着同事一会儿跑Torch,一会儿跑MXNet,一会 ...
- Tips
1)字符串转换数字 ') { ; do{ j*=;j+='); i++; }'); //实际上为读入优化的一部分 2)进制转换万能模板 #include <cstdio> using na ...