上一篇我们简单的介绍了一下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. 让CentOS系统时间同步

    CentOS系统时间同步的步骤如下: 新装的CentOS系统服务器可能设置了错误的,需要调整时区并调整时间. 如下是CentOS系统使用NTP来从一个时间服务器同步cp /usr/share/zone ...

  2. It's only too late if you decide it is. Get busy living, or get busy dying(转)

    我们的身边是不是有这样的一群人,他们心肠不坏,容易感伤,但吐槽似乎是常有的事情,看着时下电视电影,说起哪些哪些自己的泪点.一般都是吃货,别人失眠的时候,TA可能在回忆自己刚做了什么梦.我不是要去评论这 ...

  3. 9patch(.9)怎么去掉自己画上的黑点/黑线

    在自己制作.9.png图片的时候,制作之后所制作的图片上面会显示出制作的痕迹,也即是图片区域上会显示小黑点和黑线.那么为了真正的利用.9.png图片的使用效果.这些瑕疵当然是不能出现的了.因此,要想办 ...

  4. log4j 实例 , 浅析

    一.新建log4j.propperties,放在工程的src目录下. #fileAppender log4j.rootCategory = DEBUG,file,consoleAppender log ...

  5. hdu 4557 非诚勿扰 vector的应用 原来vector 可以删除指定位置元素 不过消耗大

    非诚勿扰 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submi ...

  6. easyui LinkButton

    http://www.zi-han.net/case/easyui/menu&button.html

  7. Gradle第二步骤来创建学习Task

    请下载本系列中的以下文章Github演示示例代码: git clone https://github.com/davenkin/gradle-learning.git     Gradle的Proje ...

  8. 取证学习资料DVD

    http://www.infiniteskills.com/training/learning-computer-forensics.html https://www.youtube.com/watc ...

  9. kNN(K-Nearest Neighbor)最近的分类规则

    KNN最近的规则,主要的应用领域是未知的鉴定,这一推断未知的哪一类,这样做是为了推断.基于欧几里得定理,已知推断未知什么样的特点和最亲密的事情特性: K最近的邻居(k-Nearest Neighbor ...

  10. Android采取async框架文件上传

    页面效果 须要的权限 <uses-permission android:name="android.permission.INTERNET"/> 网络訪问权限; 布局文 ...