上一篇我们简单的介绍了一下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)的更多相关文章

  1. 【八】注入框架RoboGuice使用:(Your First Injected Fragment)

        上一篇我们简单的介绍了一下RoboGuice的使用([七]注入框架RoboGuice使用:(Your First Custom Binding)),今天我们来看下fragment的注解     ...

  2. 【十一年】注入框架RoboGuice采用:(Your First Injection into a Custom View class)

    上一篇我们简单的介绍了一下RoboGuice的使用([十]注入框架RoboGuice使用:(Your First Testcase)),今天我们来看下自己定义View的注入(Custom View). ...

  3. 【十】注入框架RoboGuice使用:(Your First Testcase)

    上一篇我们简单的介绍了一下RoboGuice的使用([九]注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)),今天 ...

  4. 【十二】注入框架RoboGuice使用:(Your First Injected ContentProvider)

    上一篇我们简单的介绍了一下RoboGuice的使用([十一]注入框架RoboGuice使用:(Your First Injection into a Custom View class)),今天我们来 ...

  5. 【九】注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)

    上一篇我们简单的介绍了一下RoboGuice的使用([八]注入框架RoboGuice使用:(Your First Injected Fragment)),今天我们来看下服务(Service)和广播接受 ...

  6. 【四】注入框架RoboGuice使用:(Your First System Service Injection)

    上一篇我们简单的介绍了一下RoboGuice的使用([三]注入框架RoboGuice使用:(Your First Resource Injection)),今天我们来看下系统服务的使用注解的方法: 为 ...

  7. 【十三】注入框架RoboGuice采用:(Logging via Ln)

    上一篇我们简单的介绍了一下RoboGuice的使用([十二]注入框架RoboGuice使用:(Your First Injected ContentProvider)),今天我们来看下Log日志使用. ...

  8. 【三】注入框架RoboGuice使用:(Your First Resource Injection)

    上一篇我们简单的介绍了一下RoboGuice的使用([二]注入框架RoboGuice使用:(Your First View Injection)),今天我们来看下资源文件的使用注解的方法: 为了在Ac ...

  9. 【六】注入框架RoboGuice使用:(Singletons And ContextSingletons)

    上一篇我们简单的介绍了一下RoboGuice的使用([五]注入框架RoboGuice使用:(Your First POJO Injection)),今天我们来看下单例以及上下文单例(ContextSi ...

随机推荐

  1. quick-cocos2d-x游戏开发【4】——加入文本

    文本的加入在quick中被封装在ui类中,它能够创建EditBox.菜单以及文本,文本总得来说能够创建TTF和BMFont两种. api对于它的说明非常具体.ui.newBMFontLabel(par ...

  2. Tomcat6 Session建立机制简要

    底:  测试部门做压力测试, 结果没多久新闻,出现OutOfMemory. 查找原因,通过监视工具,查找StandardSession(org.apache.catalina.session.Stan ...

  3. SQL:多表关联采取这一纪录迄今为止最大

    笔者:iamlasong 1.需求 两个表,投递记录表和封发开拆记录表,如今想知道投递日期距最后一次封发日期天数分布情况. 对这个需求,须要先查询出投递明细,同一时候要知道相应的邮件最后一次封发情况. ...

  4. C++转让Lua

    转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/39756423 我使用的cocos2d-x版本号是2.3.3.先在一个C++projec ...

  5. 阶乘因式分解(一)(南阳oj56)

    阶乘因式分解(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描写叙述 给定两个数m,n,当中m是一个素数. 将n(0<=n<=10000)的阶乘分解质因数, ...

  6. MyEclipse建立JVM内存大小

    1.你为什么要设置JVM它的内存大小? 因为假定程序执行过JVM默认的内存大小.他们不会报告传说中的内存溢出异常. Exception in thread "main" java. ...

  7. 手机APP有三类:原生APP,WebAPP,HybridApp

    搭建 AngularJS+Ionic+Cordova 开发环境并运行一个demo 目前的手机APP有三类:原生APP,WebAPP,HybridApp:HybridApp结合了前两类APP各自的优点, ...

  8. Swing多线程编程(转)

    关键字: Swing,多线程,GUI,SwingWorker 摘要: 本文论述了怎样开发多线程的Swing程序,从而提高Swing程序的响应速度和性能.     近期,我将推出一系列研究Swing程序 ...

  9. 【原创】leetCodeOj --- Repeated DNA Sequences 解题报告

    原题地址: https://oj.leetcode.com/problems/repeated-dna-sequences/ 题目内容: All DNA is composed of a series ...

  10. WinForm、wpf、silverlight三者关系

    最近在学C#.NET,基本语法学习的差不多了,接下来准备学习图形界面设计部分.但是我目前对于.NET的WinForm.wpf.silverlight这三者的关系弄的不是很清楚,一般书中很少介绍wpf和 ...