【七】注入框架RoboGuice使用:(Your First Custom Binding)
上一篇我们简单的介绍了一下RoboGuice的使用(【六】注入框架RoboGuice使用:(Singletons
And ContextSingletons)),今天我们来看下自己定义绑定(binding)。
(一):使用自己定义绑定。我们能够绑定一个类或者一个接口到一个子类。实例或者内容提供者(provinders).
如今我们如果:
public interface IFoo {} public class Foo implements IFoo {}
该自己定义绑定同意绑定一个接口IFoo到类Foo中,然后每个注解(@Inject IFoo),就会创建一个Foo的实例。
public class MyActivity extends RoboActivity {
//How to tell RoboGuice to inject an instance of Foo ? @Inject IFoo foo;
}
(二):定义自己定义绑定:
进行自己定义绑定。我们须要创建自己的module(s)。从RoboGuice 2.0版本号開始Modules变得非常easy创建。
①:在Androidmanifset.xml中注冊Modules
②:创建继承AbstractModule的类
2.1:在Androidmanifset.xml中注冊Modules
在Androidmanifset.xml中,application标签中加入一些meta-data标签字段以及modules全名,例如以下所看到的:
<application ...>
<meta-data android:name="roboguice.modules"
android:value="com.example.MyModule,com.example.MyModule2" />
</application>
2.2:创建继承AbstractModule的类
每个modules必须在创建之前必需要注冊,这些所有会通过反射进行訪问。看以下的实例代码:
package com.example; public class MyModule extends AbstractModule {
//a default constructor is fine for a Module public void bind() {
bind(IFoo.class).to(Foo.class);
}
} public class MyModule2 extends AbstractModule {
//if your module requires a context, add a constructor that will be passed a context.
private Context context; //with RoboGuice 3.0, the constructor for AbstractModule will use an `Application`, not a `Context`
public MyModule( Context context ) {
this.context = context;
} public void bind() {
bind(IFoo.class).toInstance( new Foo(context));
}
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
【七】注入框架RoboGuice使用:(Your First Custom Binding)的更多相关文章
- 【八】注入框架RoboGuice使用:(Your First Injected Fragment)
上一篇我们简单的介绍了一下RoboGuice的使用([七]注入框架RoboGuice使用:(Your First Custom Binding)),今天我们来看下fragment的注解 ...
- 【十一年】注入框架RoboGuice采用:(Your First Injection into a Custom View class)
上一篇我们简单的介绍了一下RoboGuice的使用([十]注入框架RoboGuice使用:(Your First Testcase)),今天我们来看下自己定义View的注入(Custom View). ...
- 【十】注入框架RoboGuice使用:(Your First Testcase)
上一篇我们简单的介绍了一下RoboGuice的使用([九]注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)),今天 ...
- 【十二】注入框架RoboGuice使用:(Your First Injected ContentProvider)
上一篇我们简单的介绍了一下RoboGuice的使用([十一]注入框架RoboGuice使用:(Your First Injection into a Custom View class)),今天我们来 ...
- 【九】注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)
上一篇我们简单的介绍了一下RoboGuice的使用([八]注入框架RoboGuice使用:(Your First Injected Fragment)),今天我们来看下服务(Service)和广播接受 ...
- 【四】注入框架RoboGuice使用:(Your First System Service Injection)
上一篇我们简单的介绍了一下RoboGuice的使用([三]注入框架RoboGuice使用:(Your First Resource Injection)),今天我们来看下系统服务的使用注解的方法: 为 ...
- 【十三】注入框架RoboGuice采用:(Logging via Ln)
上一篇我们简单的介绍了一下RoboGuice的使用([十二]注入框架RoboGuice使用:(Your First Injected ContentProvider)),今天我们来看下Log日志使用. ...
- 【三】注入框架RoboGuice使用:(Your First Resource Injection)
上一篇我们简单的介绍了一下RoboGuice的使用([二]注入框架RoboGuice使用:(Your First View Injection)),今天我们来看下资源文件的使用注解的方法: 为了在Ac ...
- 【六】注入框架RoboGuice使用:(Singletons And ContextSingletons)
上一篇我们简单的介绍了一下RoboGuice的使用([五]注入框架RoboGuice使用:(Your First POJO Injection)),今天我们来看下单例以及上下文单例(ContextSi ...
随机推荐
- 一步一步学android之事件篇——触摸事件
触摸事件顾名思义就是触摸手机屏幕触发的事件,当用户触摸添加了触摸事件的View时,就是执行OnTouch()方法进行处理,下面通过一个动态获取坐标的例子来学习OnTouchListener事件,效果如 ...
- (九)通过几段代码,理清angularJS中的$injector、$rootScope和$scope的概念和关联关系
$injector.$rootScope和$scope是angularJS框架中比較重要的东西,理清它们之间的关系,对我们兴许学习和理解angularJS框架都很实用. 1.$injector事实上是 ...
- Acquire and Release Semantics
An operation has acquire semantics if other processors will always see its effect before any subsequ ...
- 初探Java8中的HashMap(转)
HashMap是我们最常用的集合之一,同时Java8也提升了HashMap的性能.本着学习的原则,在这探讨一下HashMap. 原理 简单讲解下HashMap的原理:HashMap基于Hash算法,我 ...
- 无状态会话bean(3)---远程业务接口(没有排版)
迄今为止,我们仅仅讨论了使用一个本地业务接口的会话bean.在这样的情况下.本地意味着仅仅能由执行在同一个应用程序server实例的JavaEE组件声明会话bean的依赖性.比如.远程client不可 ...
- iOS Dev (59) 高度自适应的UITextView
iOS Dev (59) 高度自适应的UITextView 作者:阿锐 地址:http://blog.csdn.net/prevention - 例如以下 _inputTextView 为一个 UIT ...
- 观察者模式(Observer Patterns)
今天学习了观察者模式,做个总结,方便以后回想. 首先是定义:观察者模式就是定义对象之间一对多的依赖关系,当一个对象状态发生改变时,全部依赖他的对象都收到推送消息并自己主动更新做出改变. 我的理解:生活 ...
- 添加xml文件编辑语法提示
找到Struts的lib目录 找到struts2-core-文件并解压开 这个struts.dtd文件才是我们需要添加的文件 双击XML Catalog 点击ADD Key中复制粘贴D:\web\st ...
- Nutch+Lucene搜索引擎开发实践
网络拓扑 图 1 网络拓扑图 安装Java JDK 首先查看系统是否已经安装了其它版本号的JDK,假设有,先要把其它版本号的JDK卸载. 用root用户登录系统. # rpm-qa|grep gcj ...
- 在MyEclipse8.5中配置Tomcat6.0服务器
一.单击工具栏的的黑小三角,选择—>Configure Server,出现首选项对话框,在对话框的左边框中找到MyEclipse—>Application Servers下找到Tomcat ...