Android RoboGuice使用指南(11) Scopes

缺省情况下,Guice每次都创建类的一个新的实例对象给需要该类实例的地方 。可以使用Scopes来修改这个缺省行为,Scope允许在一定范围内重用类实例。 Roboguice中常用的有两种:

@Singleton 整个Application生命周期中使 用同一实例对象

@ContextScoped 同一个Context(如Activity)中共享某一实 例对象。

使用Scope 的方法为使用相应的标记,如:

@Singleton
public class InMemoryTransactionLog implements TransactionLog {
 // everything here should be threadsafe!
 }

或者在Module中使用bind 语句:

bind

(TransactionLog.class)
 .to(InMemoryTransactionLog.class)
 .in(Singleton.class);

如果使用@Provides,可以有:

@Provides @Singleton
TransactionLog provideTransactionLog() {
...
}

如果某个类型使用某个你不想使用的Scope标记,可以将其绑定到 Scopes.NO_SCOPE取消这个Scope定义。

查看全套文章:http://www.bianceng.cn/OS/extra/201301/34950.htm

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索对象
, class
, 实例
, 标记
, singleton
scope
roboguice、roboguice3.0 使用、robobinding android、android roboguide、the scopes trial译文,以便于您获取更多的相关知识。

时间: 2024-08-19 12:10:34

Android RoboGuice使用指南(11) Scopes的相关文章

Android RoboGuice使用指南(13) RoboGuice功能描述

前面在Android RoboGuice 使用指南(1):概述 对应Roboguice做了简要的介绍 ,之后介绍了Google Guice的基本用法,Roboguice是基本Android和Google Guice开发的适用于Android平台的Dependency Injection 开发包,下图为使用 Roboguice开发应用的基本框图: Android应用程序可以直接使用Google Guice来为普通类进行注入操作,而对 和Android平台相关的类如Activity,Context,

Android RoboGuice使用指南(15) Inject Context

在Android应用程序中,很多地方需要引用到Context对象(Activity, Application,Service等).Roboguice 使得引用Context对象变得非常容易. 可以参见下面例子,这里定义一个不在Activity中的类ContextInfo,需 要引用Context对象: class ContextInfo{ final Context context; @Inject ContextInfo(Context context){ this.context=conte

Android RoboGuice使用指南(14) Inject View

在例子Android RoboGuice 使用指南(2):第一个例子Hello World 介绍了使用 Roboguice开发的基本步骤: 创建一个RoboApplication 的子类GuiceApplication,GuiceApplication为 Appliacation的子类,修改AndroidManifest.xml,将Application 的name 指向这 个类. 将原先由Activity派生的类基类改为RoboActivity(或其它相关 Activity). 如果有需要的话

Android RoboGuice使用指南(10) Just-in-time Bindings

Injector 通过检查bindings 定义来创建某个类型的实例对象.定义在Module 中的绑定称为"明确声明绑定(Explicit bindings".Injector 会首先使用带 有Explicit Bindings为某个类型创建实例对象. 当但某个类型没有明确定义绑 定时,Injector 试图构造"即时绑定(Just-in-time Bindings),JIT Bindings 也成为隐含绑定(implicit bindings). Eligible Cons

Android RoboGuice使用指南(6) Instance Bindings

我们在前面例子Android RoboGuice 使用指南(4):Linked Bindings 时为简单 起见,定义MyRectangle和MySquare时为它们定义了一个不带参数的构造函数,如 MyRectangle的如下: public class MyRectangle extends Rectangle{ public MyRectangle(){ super(50,50,100,120); } public MyRectangle(int width, int height){ s

Android RoboGuice使用指南(5) Binding Annotations

有些情况需要将同一类型映射到不同的类实现,还是使用绘图的例 子. IShape, Rectangle, MyRectangle, MySquare,有如下继承关系: 我们可能需要将IShape 同时映射到 MyRectangle 和MySquare ,这时可以使用Binding Annotation 来实现. 这时使 用类型和annotation (标注)可以唯一确定一个Binding.Type 和annotation 对 称为Key(键). 为了同时使用MyRectangle和MySequar

Android RoboGuice使用指南(19) 发送接收Events

Roboguice 提供了对Context 生命周期相关的事件的send 和receive ,系统缺 省支持的事件为: OnActivityResultEvent OnConfigurationChangedEvent OnContentC hangedEvent OnContentViewAvailableEvent OnCreateEvent OnDestroyEv ent OnNewIntentEvent OnPauseEvent OnRestartEvent OnResumeEvent<

Android RoboGuice使用指南(18) Inject Resources

Roboguice 对访问res 目录下各种资源drawable, arrary, string 等也提供 了注入支持.可以通过@InjectResource 很方便的应用所需资源. 本例修 改Android ApiDemos示例解析(48):Content->Resources->Resources 使 用Inject Resource方法来访问资源. public class InjectResourceDemo extends RoboActivity { @InjectView (R.

Android RoboGuice使用指南(17) Inject Extra

使用Intent 启动一个Activity,Service等时,可以通过putExtra 传送数据 ,被触发的Activity,Service可以使用getIntent()的getExtras 取的Extra的 Bundle ,然后再根据Extra的键值(Key)取的对应的参数值. RoboGuice提 供了一个简洁的方法来取得 这些Extra 值,通过@InjectExtra 标记. 本 例使用两个Activity,InjectExtraDemo 用来触发InjectExtraReceiver