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'] ...
随机推荐
- SSH ProxyCommand
简单的说就是SSH层面的代理服务器,实现通过跳板机执行SSH相关命令到目标机器的代理服务.
- SQL2005 表分区亲测
--增加文件组 alter database Test add filegroup [FG1] go alter database Test add filegroup [FG2] GO alter ...
- Eclipse 调试Bug之使用断点的七大技巧
原文链接:http://xiaohuafyle.iteye.com/blog/1705494 调试竟然有这么多技巧,亏我以前竟不知道... Eclipse这个开发工具为我们调试bug提供了非常便利 ...
- TOMCAT-报错The BASEDIR environment variable is not defined correctly
<span style="font-size:18px;">The BASEDIR environment variable is not defined correc ...
- 白银5kg
领悟: 1.其实大势涨也好,跌也好,如果我们知道价格到哪里会受阻或反弹,能出有出利润的空间,我们就可以做单.
- 【大数据技巧】日均2TB日志数据在线快速处理之法
[大数据技巧]日均2TB日志数据在线快速处理之法 http://click.aliyun.com/m/8958/
- centos 6.5 yum安装 mysql 5.6
1. 查看系统里面有没有mysql 的repo yum repolist all | grep mysql 2. 如果没有发现,则需要配置repo 注意,如果要使用5.7 或者其他任何版本,只能有 ...
- MYSQL 按照字母排序查询
select id vKey, name vValue from ib_brand order by convert(name USING gbk) COLLATE gbk_chinese_ci a ...
- mac 关于使用protobuf出现ld: symbol(s) not found for architecture x86_64的问题
主要是编译时没有添加protobuf库文件 g++ -o Writer.o lm.helloworld.pb.cc Writer.cpp -L/usr/local/lib -lprotobuf
- Codeforces Round #260 (Div. 2)
A. Laptops 题目意思: 给定n台电脑,第i台电脑的价格是ai ,质量是bi ,问是否存在一台电脑价格比某台电脑价格底,但质量确比某台电脑的质量高,即是否存在ai < aj 且 bi & ...