【七】注入框架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));
    }
}

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

时间: 2024-07-28 16:11:24

【七】注入框架RoboGuice使用:(Your First Custom Binding)的相关文章

【十四】注入框架RoboGuice使用总结

在我们平时开发Android项目的时候例如经常需要使用各种View控件,然后进行声明,findViewById,并且进行强转.每次都要写这样的代码就显得非常繁琐,并且容易出错哦.那么针对这种情况且不限定于以上的这类情况,Dependency injection 可以大大降低了类之间的依赖性,可以通过annotation (Java)描述类之间的依赖性,避免了直接调用类似的构造函数或是使用Factory来参加所需的类,从而降低类或模块之间的耦合性,以提高代码重用并增强代码的可维护性.Google

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

上一篇我们简单的介绍了一下RoboGuice的使用([七]注入框架RoboGuice使用:(Your First Custom Binding)),今天我们来看下fragment的注解 (一):简介:和Activity一样,我们也同样可以在Fragment中使用注解,你需要一下的步骤. ①:编写一个继承与RoboFragment的Fragment; ②:设置你的布局(如果使用View注入) ③:注入你的View空间,资源文件(Resources),对象等等 (二):例子: public clas

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

上一篇我们简单的介绍了一下RoboGuice的使用([十]注入框架RoboGuice使用:(Your First Testcase)),今天我们来看下自定义View的注入(Custom View). 在开始本文之前,你先要熟悉普通Java对象的注入(点击进入).在RoboGuice 3.0版本中你同样给自定义View(Custom View)进行诸如. class MyView extends View { @Inject Foo foo; @InjectView(R.id.my_view) T

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

上一篇我们简单的介绍了一下RoboGuice的使用([九]注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)),今天我们来看下测试用例(TestCase)的注入 RoboGuice使得我们更加容易实现可测试的Android应用程序,本文章就来详细解说下:当我们测试的时候,如何编写测试用例,已经从RoboGuice中获益.本文章使用Android Robolectric,适合大部分用Android标准测试的情况.

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

上一篇我们简单的介绍了一下RoboGuice的使用([十一]注入框架RoboGuice使用:(Your First Injection into a Custom View class)),今天我们来看下内容提供者(ContentProvider)的注入. 和Robo*Activities一样,RoboContentProviders通过RoboGuice也能自动获得注入,为了简便我们可以注入 authority URI,这时我们需要定义下面自己的module: public class Con

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

上一篇我们简单的介绍了一下RoboGuice的使用([十二]注入框架RoboGuice使用:(Your First Injected ContentProvider)),今天我们来看下Log日志使用. Android应用通过会使用内置的android.util.log在Android控制台上面打印日志信息.RoboGuice也提供了另外的日志管理,你可能会想要使用. (一):  RoboGuice日志管理和通常的Log差不多,但也同样具备以下优点: ①:对于发布包来说,Debug and ver

【五】注入框架RoboGuice使用:(Your First POJO Injection)

上一篇我们简单的介绍了一下RoboGuice的使用([四]注入框架RoboGuice使用:(Your First System Service Injection)),今天我们来看下普通Java对象的使用注解的方法: (一)为了在Activity中普通Java对象使用注解,必须实现以下两个步骤: ①:创建继承RoboActivity的Activity ②:使用 @Inject来进行注解POJO(普通java对象) 在前面我们介绍了View控件,资源文件(Resources),系统服务(Syste

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

上一篇我们简单的介绍了一下RoboGuice的使用([三]注入框架RoboGuice使用:(Your First Resource Injection)),今天我们来看下系统服务的使用注解的方法: 为了在Activity中系统服务使用注解,必须实现以下两个步骤: ①:创建继承RoboActivity的Activity ②:使用 @Inject来进行注解系统服务 下面我们来实现一个例子,和上一篇文章例子差不多,我们需要进行实现系统服务,我们同样创建一个继承RoboActivity的Activity

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

上一篇我们简单的介绍了一下RoboGuice的使用([八]注入框架RoboGuice使用:(Your First Injected Fragment)),今天我们来看下服务(Service)和广播接受者(BroadCast Receiver)的注入 (一):和Robo*Activities一样,RoboServices和RoboIntentServices通过RoboGuice也自动接受注入. 下面是一个使用RoboGuice注入的android service的例子: public class