【七】注入框架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 ...
随机推荐
- Spring——AOP配置时的jar包异常
首先:这不是SSH整合的,这是单独配置Spring AOP的一个小例子. 所需要的jar包:如图: 我在这里出现的两个问题: 1.没有导入asm的jar包. 所报的异常为: java.lang.Cla ...
- ECSHOP分类页面筛选功能(按分类下子分类和品牌筛选)
其实分类页面里面本来就有相关的品牌.属性.分类的筛选功能在category.php和模板加上相应的功能即可 1.读出当前分类的所有下级分类 $chlidren_category = $GLOBALS[ ...
- SecureCRT学习之道:SecureCRT常用快捷键设置与字体设置方法
1:如果不想每次登陆都输入密码,可以在你打开的session里邮件session option->login action 选中automate logon 双击ogin 和assword分别输 ...
- Radmin远程控制软件
Radmin远程控制软件 日期:2015-08-20 作者:lujl Radmin是一款快速的远程控制软件,可以用来远程管理公司或个人计算机来实现远程办公.你可以通过鼠标和键盘来控制远程的 ...
- iOS很重要的 block回调
刚刚进入ios开发行业,发现开发中要用到大量的block回调,由此可见它的重要性.学习它之前我也是网上找的资料,推荐这篇文章http://blog.csdn.net/mobanchengshuang/ ...
- Windows Phone开发(44):推送通知第二集——磁贴通知
原文:Windows Phone开发(44):推送通知第二集--磁贴通知 前面我们说了第一个类型--Toast通知,这玩意儿不知大家是不是觉得很新鲜,以前玩.NET编程应该没接触过吧? 其实这东西绝对 ...
- android的ViewPager和Animation有些使用(二)
Animation部分 android的animation分scale,rotate,tranlateAnimation,alpha这些类型的 start animation这里有几种方法: < ...
- [Cocos2d-x v3.x]浅谈容器Vector
转载请注明来自:star特530的CSDN博客 http://blog.csdn.net/start530/article/details/19170853 前两天有人问我说在3.0 beta2版本号 ...
- 分析java.lang.NullPointerException thrown in RelativeLayout measure()
典型的再现环境 模型: Sony Ericsson Android version: 2.3.4 StackTrace: E/AndroidRuntime( 3579): FATAL EXCEPTIO ...
- 802.11(wi-fi)的PHY层(编码与调制方法)
版本概要: 802.11-2007是目前的基础版本,之前的过时版本不考虑. 2009是较新的版本,就是目前最普及的802.11n.(100Mb/s) 2012就是传说中的802.11ac,工作在5G, ...