首先介绍一下如果将Guice 和RoboGuice 的库添加到项目中。
- 下载RoboGuice和guice-2.0-no_aop.jar(not guice-3.0),或者下载
- 创建一个新Android项目,比如GuiceDemo,目标平台Android1.5以上。
- 一般可以在该项目下添加一个lib目录,将两个jar文件拷到lib目录下,然后通过: Project > Properties > Java Build Path > Libraries > Add External JARs
添加了对应guice 和roboguice库的引用之后,就可以开始编写第一个使用roboguice 的例子。
使用roboguice 的步骤:
1. 创建一个RoboApplication 的子类GuiceApplication,GuiceApplication为Appliacation的子类,因此需要修改AndroidManifest.xml,将Application 的name 指向这个类。可以参见Android简明开发教程九:创建应用程序框架
<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>
2. 在这个简单的例子中,它使用的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>
我们定义了一个TextView ,它的id为hello.
假定这个应用使用一个IGreetingService ,它有一个方法getGreeting() 返回一个字符串,至于IGreetingService 如何实现,GuideDemo 不需要关心。
Dependency injection 设计模式的一个核心原则为: Separate behavior from dependency resolution. 也就说将应用需要实现的功能和其所依赖的服务或其它对象分离。 对本例来说GuiceDemo只要知道它依赖于IGreetingService 服务,至于IGreetingService有谁实现GuiceDemo并不需要知道。
在Roboguice 中使用@Inject 来表示这种依赖关系。
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标注greetingServce依赖于IGreetingService服务
使用@InjectView表示helloLabel 依赖于R.id.hello (XML)
代码中没有创建greetingServce 对象的代码(如 new xxx()) 和为helloLabel 赋值的代码。这些值都可以Roboguice 自动创建和赋值注入(Inject)到变量中。
为了说明问题,我们在代码中添加两个对getGreetings的实现,一个为HelloWorld, 一个为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";
}
}
3. 到这里,你可能有些困惑,RoboGuice怎么知道使用那个类(HelloWorld或是HelloChina)为GuiceDemo中的greetingServce 赋值呢?这是通过在Module 中定义binding 来实现的。
在项目中添加一个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 ,对比一下:
通过改变binding ,GuiceDemo 显示了不同的结果,GuiceDemo不依赖于具体的实现,可以非常方便的改变接口的实现而无需更改GuiceDemo的代码。大大降低了类于类之间的耦合性。
后面将逐个介绍Guice和RoboGuice支持的Binding类型和用法(Guice)以及与android 平台相关的Dependency injection (RoboGuice)
本例下载(含 roboguice 库)
分享到:
相关推荐
Delphi第一个源码:HelloWorld例子,HelloWorld是个很经典的初学例子,被默认为学习和种语言的第一个小程序,用以在屏幕上输出“HelloWorld”,或是控制台、或是弹出窗口的形式,本程序就是Delphi中的HelloWorld,...
笨鸟学Android开发(1):HelloWorld
std::cout << "Hello, world!" << std::endl; std::operator(std::cout, "Hello, world!"); // friend cout是模板类的实例 std::cout.operator(std::endl); // member endl是函数模板指针 std::...
Android第一行代码源码 ...HelloWorldAndroid第一行代码源码 HelloWorldAndroid第一行代码源码 HelloWorldAndroid第一行代码源码 HelloWorldAndroid第一行代码源码 HelloWorldAndroid第一行代码源码 HelloWorld
第一个DWR HelloWorld例子(附:详细说明) 第一个DWR HelloWorld例子(附:详细说明) 第一个DWR HelloWorld例子(附:详细说明)
Android demo Android helloworld 源码
Stuido中使用JNI例子HelloWorld,带制作so文件,并在别的项目中调用
源码,Intellij Idea12开发android 第一个程序HelloWorld, 图文教程:http://blog.csdn.net/juyangjia/article/details/9491781
1、下载解压缩之后,使用Eclipse导入该工程 2、然后export该工程为war档,并且把war档放到你本地Tomcat的webapps目录下 3、在Tomcat的bin目录启动Tomcat服务器 4、打开IE在地址栏输入...
hello world
android helloWorld程序
hello world视频教程,希望能够学习者提供帮助,实现对Android基础知识的掌握与理解,为后续学习做好铺垫,实现Android知识的灵活运用
android,helloworld,例子
NULL 博文链接:https://fengfan2008.iteye.com/blog/509342
嵌入式Android项目设计与开发 第一章 Android开发概述 —— HelloWorld HelloWorld 主要内容: 1.建立新的虚拟机 2.建立新的项目 3.编译和加载程序 AndroidStudio界面 谢谢 请专家批评指正!
ssh helloworld小例子 ssh helloworld小例子 ssh helloworld小例子 ssh helloworld小例子 ssh helloworld小例子
JBPM 例子 Hello World 例子 希望对大家有用
新建Android项目选择菜单file->new->project进入如下界面: 2、 选择Android下的Android project,点击Next,进入如下界面: Project name:HelloAndroid 选择Build Target Application name:Hello,Android, Package ...
图1 界面要求 实验思路 实验002:第一个可运行试验HelloAndroid全文共2页,当前为第1页。本实验存在两个难点:首先是Android项目的生成和运行,这里在基础实验《实验准备》章节中有所涉及,本实验侧重于与Android...
iphone 开发入门例子 helloworld