上一篇我们简单的介绍了一下RoboGuice的使用(【六】注入框架RoboGuice使用:(Singletons
And ContextSingletons)
),今天我们来看下自己定义绑定(binding)。

(一):使用自己定义绑定。我们能够绑定一个类或者一个接口到一个子类。实例或者内容提供者(provinders).

如今我们如果:

  1. public interface IFoo {}
  2.  
  3. public class Foo implements IFoo {}

该自己定义绑定同意绑定一个接口IFoo到类Foo中,然后每个注解(@Inject IFoo),就会创建一个Foo的实例。

  1. public class MyActivity extends RoboActivity {
  2. //How to tell RoboGuice to inject an instance of Foo ?
  3.  
  4. @Inject IFoo foo;
  5. }

(二):定义自己定义绑定:

进行自己定义绑定。我们须要创建自己的module(s)。从RoboGuice 2.0版本号開始Modules变得非常easy创建。

①:在Androidmanifset.xml中注冊Modules

②:创建继承AbstractModule的类

2.1:在Androidmanifset.xml中注冊Modules

在Androidmanifset.xml中,application标签中加入一些meta-data标签字段以及modules全名,例如以下所看到的:

  1. <application ...>
  2. <meta-data android:name="roboguice.modules"
  3. android:value="com.example.MyModule,com.example.MyModule2" />
  4. </application>

2.2:创建继承AbstractModule的类

每个modules必须在创建之前必需要注冊,这些所有会通过反射进行訪问。看以下的实例代码:

  1. package com.example;
  2.  
  3. public class MyModule extends AbstractModule {
  4. //a default constructor is fine for a Module
  5.  
  6. public void bind() {
  7. bind(IFoo.class).to(Foo.class);
  8. }
  9. }
  10.  
  11. public class MyModule2 extends AbstractModule {
  12. //if your module requires a context, add a constructor that will be passed a context.
  13. private Context context;
  14.  
  15. //with RoboGuice 3.0, the constructor for AbstractModule will use an `Application`, not a `Context`
  16. public MyModule( Context context ) {
  17. this.context = context;
  18. }
  19.  
  20. public void bind() {
  21. bind(IFoo.class).toInstance( new Foo(context));
  22. }
  23. }

版权声明:本文博客原创文章。博客,未经同意,不得转载。

【七】注入框架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. 用KnockoutJS实现ToDoMVC代码分析

    体验地址 Knockout 版todo web app在线体验 http://todomvc.com/examples/knockoutjs/ 源码地址 项目源码地址,此地址包含了各种JS框架实现的t ...

  2. Java-UrlRewrite中文api文档

    安装 1. 下载jar包, 并加入到WEB-INF/lib下 2. 在WEB-INF/web.xml中增加下面的配置 <filter> <filter-name>UrlRewr ...

  3. Android中TweenAnimation四种动画切换效果

    点击每个按钮都会有对应的动画显示 activity代码: package com.tmacsky; import android.app.Activity; import android.os.Bun ...

  4. macbook连接linuxserver后不显示matlab桌面[问题]

    macbook 登录远程linuxserver.Macbook今天,系统版本号10.9.4.这是更新,打开matlab当提醒x11不存在.一个接着安装Xquarz2.7.6. matlab它可以在本地 ...

  5. SSO(Single Sign On)系列(一)--SSO简单介绍

    任何类型的站点,到达一定规模之后一定会存在这种问题:比方我们有N个系统.传统方式下我们就须要有N对不同的username和password,本来这些系统的开发都能为我们带来良好的效益,用户在用的时候并 ...

  6. Android自己的自动化测试Monkeyrunner和用法示例

    眼下android SDK在配有现成的测试工具monkey 和 monkeyrunner两. 也许我们不看一样的兄弟名字.但事实是完全跑了两个完全不同的工具.在测试的不同区域的应用程序.总体,monk ...

  7. tshark命令行的使用(转)

    tshark是wireshark的一个命令行工具用于抓包分析: 主要参数如下: 1. 抓包接口类 -i 设置抓包的网络接口,不设置则默认为第一个非自环接口. -D 列出当前存在的网络接口.在不了解OS ...

  8. Eclipse中为什么创建DynamicWebProject后没有默认的web.xml文件?

    在Eclipse中新建DynamicWebProject的时候不要直接点"完毕",在下一步有个勾选项(Generate web.xml deployment descriptor) ...

  9. LeetCode: Multiply Strings. Java

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  10. Blend4精选案例图解教程(三):一键拖拽

    原文:Blend4精选案例图解教程(三):一键拖拽 拖拽效果,常规实现方法是定义MoveLeftDwon.MoveLeftUp.MouseMove事件,在Blend的世界里,实现对象的拖拽,可以不写一 ...