在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ 第一個(gè)例子 Hello World
Just-in-time Bindings
Binding Annotations
Standard Injection
第一個(gè)例子 Hello World
Bindings 概述
Linked Bindings
如何綁定 generic 類型
@Provides Methods
RoboGuice 功能描述
概述
綜合示例 Astroboy
Inject Resources
Instance Bindings
Inject 自定義 View
Scopes
Provider Bindings
Untargetted Bindings
Inject Extra
第一個(gè)例子 Hello World
Inject Context
發(fā)送接收 Events
Inject View

第一個(gè)例子 Hello World

首先介紹一下如果將 Guice 和 RoboGuice 的庫添加到項(xiàng)目中。

  1. 下載 RoboGuice 和 guice-2.0-no_aop.jar(not guice-3.0), 或者下載
  2. 創(chuàng)建一個(gè)新 Android 項(xiàng)目,比如 GuiceDemo,目標(biāo)平臺(tái) Android1.5 以上。
  3. 一般可以在該項(xiàng)目下添加一個(gè) lib 目錄,將兩個(gè) jar 文件拷到 lib目錄下,然后通過: Project > Properties > Java Build Path > Libraries > Add External JARs

http://wiki.jikexueyuan.com/project/android-roboguice/images/1.png" alt="" />

添加了對(duì)應(yīng) guice 和 roboguice 庫的引用之后,就可以開始編寫第一個(gè)使用 roboguice 的例子。

使用roboguice 的步驟:

  1. 創(chuàng)建一個(gè) RoboApplication 的子類 GuiceApplication,GuiceApplication 為 Appliacation 的子類,因此需要修改 AndroidManifest.xml,將 Application 的 name 指向這個(gè)類。可以參見 Android 簡(jiǎn)明開發(fā)教程九:創(chuàng)建應(yīng)用程序框架
<application android:name=”GuiceApplication”
android:icon=”@drawable/icon” android:label=”@string/app_name”>
<activity android:name=”.GuiceDemo”
android:label=”@string/app_name”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>

</application>
  1. 在這個(gè)簡(jiǎn)單的例子中,它使用的 Layout 定義如下:
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
>
<TextView
android:id=”@+id/hello”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello”
/>
</LinearLayout>

我們定義了一個(gè) TextView ,它的 id 為 hello.

假定這個(gè)應(yīng)用使用一個(gè) IGreetingService ,它有一個(gè)方法 getGreeting() 返回一個(gè)字元串,至于 IGreetingService 如何實(shí)現(xiàn),GuideDemo 不需要關(guān)心。

http://wiki.jikexueyuan.com/project/android-roboguice/images/2.png" alt="" />

Dependency injection 設(shè)計(jì)模式的一個(gè)核心原則為: Separate behavior from dependency resolution. 也就說將應(yīng)用需要實(shí)現(xiàn)的功能和其所依賴的服務(wù)或其它對(duì)象分離。 對(duì)本例來說 GuiceDemo 只要知道它依賴于 IGreetingService 服務(wù),至于 IGreetingService 有誰實(shí)現(xiàn)GuiceDemo 并不需要知道。

在 Roboguice 中使用 @Inject 來表示這種依賴關(guān)系。

public class GuiceDemo extends RoboActivity  {

 @InjectView (R.id.hello) TextView helloLabel;
 @Inject IGreetingService greetingServce;

 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 helloLabel.setText(greetingServce.getGreetings());
 }
}
  • 使用 RoboGuice 的 Activity 需要從 RoboActivity 派生 (RoboActivity 為 Activity 的子類).
  • 使用 @Inject 標(biāo)注 greetingServce 依賴于 IGreetingService 服務(wù)
  • 使用 @InjectView 表示 helloLabel 依賴于 R.id.hello (XML)

代碼中沒有創(chuàng)建 greetingServce 對(duì)象的代碼(如 new xxx()) 和為 helloLabel 賦值的代碼。這些值都可以 Roboguice 自動(dòng)創(chuàng)建和賦值注入(Inject) 到變數(shù)中。

為了說明問題,我們?cè)诖a中添加兩個(gè)對(duì) getGreetings 的實(shí)現(xiàn),一個(gè)為 HelloWorld, 一個(gè)為 HelloChina:

public class HelloChina implements IGreetingService{

 @Override
 public String getGreetings() {
 return "Hello,China";
 }

}

public class HelloWorld implements IGreetingService{

 @Override
 public String getGreetings() {
 return "Hello,World";
 }

}
  1. 到這裡,你可能有些困惑,RoboGuice 怎麼知道使用那個(gè)類(HelloWorld 或是 HelloChina)為 GuiceDemo 中的 greetingServce 賦值呢?這是通過在 Module 中定義 binding 來實(shí)現(xiàn)的。

項(xiàng)目中添加一個(gè) GreetingModule (從 AbstractAndroidModule 派生)重載 configure 方法:

public class GreetingModule extends AbstractAndroidModule{

@Override
protected void configure() {
bind(IGreetingService.class).to(HelloWorld.class);
//bind(IGreetingService.class).to(HelloChina.class);

}

}

將 IGreetingService 綁定到 HelloWorld 類。

然后在 GuiceApplication 的 addApplicationModules 添加上述模塊:

public class GuiceApplication extends RoboApplication {

 protected void addApplicationModules(List<Module> modules) {
 modules.add(new GreetingModule());
 }

}

可以將 GreetingModule 綁定改為 HelloChina ,對(duì)比一下:

http://wiki.jikexueyuan.com/project/android-roboguice/images/3.png" alt="" />

通過改變 binding ,GuiceDemo 顯示了不同的結(jié)果,GuiceDemo 不依賴于具體的實(shí)現(xiàn),可以非常方便的改變介面的實(shí)現(xiàn)而無需更改 GuiceDemo的代碼。大大降低了類于類之間的藕合性。

后面將逐個(gè)介紹 Guice 和 RoboGuice 支持的 Binding 類型和用法(Guice) 以及與 android 平臺(tái)相關(guān)的 Dependency injection (RoboGuice)