android roboguice 笔记

来源:互联网 发布:android 数据共享方式 编辑:程序博客网 时间:2024/05/02 00:46

roboguice 2.0.jar有三个依赖包:guice-3.0-no_aop.jar,javax.inject-1.jar,jsr305-1.3.9.jar
roboguice 1.*.jar有一个依赖包:guice-2.0-no_aop.jar

2.0比1.*的优势是:
  提高了稳定性
  支持Fragment
  更简洁易用

2.0与1.*的区别是:

  2.0不在对Application进行重写,原本1.*含有的RoboApplication在2.0中已不存在

  2.0的模块绑定需要继承AbstractModules,原本1.*中的AbstractAndroidModules不在存在

  2.0增加了对Fragment的支持,所以进行android3.0以上版本的开发的时候,2.0可以更好地支持此属性。

  

在开发应用时,有个原则是模块化,而模块与模块之间 要尽可能的降低耦合,
基于框架降低耦合的一个典型模式就是 依赖注入,在spring框架,谷歌的guice,以及谷歌基于android平台的roboguice框架,
都具有依赖注入功能,有了框架的依赖注入,我们就不必使用构造函数或者工厂方法来实例化对象,而是由框架本身 根据类与类之间所描述的关系,自动注入。spring的反转控制的本质就是 自下而上的依赖注入。
当然guice和roboguice有些地方是用到了一些工厂方法的,但是程序不会依赖于工场方法来创建对象,只是框架所提供的Api对外部使用的一种支持,

 

RoboGuice是Guice根据android平台设计的框架,较少了繁琐的查找代码,对象实例化等工作。下面分11个步骤全面说明RoboGuice的特性:

   

1.当一个类 有默认构造函数时,支持 
  @ Inject ClassName demo;


2.支持Android资源的注入:
  @InjectView(R.id.XXX) TextView name;
  @InjectResource(R.drawable.icon) Drawable icon;


3.支持Android标准API的注入:


  @Inject ContentResolver contentResolver;
  @Inject AssetManager assetManager;
  @Inject Resources resources;
  @Inject LocationManager locationManager;
  @Inject WindowManager windowManager;
  @Inject LayoutInflater layoutInflater;
  @Inject ActivityManager activityManager;
  @Inject PowerManager powerManager;
  @Inject AlarmManager alarmManager;
  @Inject NotificationManager notificationManager;
  @Inject KeyguardManager keyguardManager;
  @Inject SearchManager searchManager;
  @Inject Vibrator vibrator;
  @Inject ConnectivityManager connectivityManager;
  @Inject WifiManager wifiManager;
  @Inject InputMethodManager inputMethodManager;
  @Inject SensorManager sensorManager;
  @Inject Application application;
  @Inject Context context;


4.多种实现的绑定:


例如:
接口:

public interface PhoneService{
public void call();
}

实现:
实现1:
public class PhoneStyleOne implements PhoneService{
public PhoneStyleOne() {
System.out.println("PhoneStyleOne");
}

@Override
public void call() {
System.out.println("PhoneStyleOne:PhoneStyleOne");
}
}

实现2:
public class PhoneStyleTwo implements PhoneService {
public PhoneStyleTwo() {
System.out.println("PhoneStyleTwo");
}

@Override
public void call() {
System.out.println("PhoneStyleTwo:PhoneStyleTwo");
}

}

该种情况下,有两种绑定方式:

第一种:自定义注解,

定义注解:

@BindingAnnotation
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD }) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface PO {
//....
}

@BindingAnnotation
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD }) 
@Retention(RetentionPolicy.RUNTIME) 
public @interface PW {
//....
}

绑定:
bind(PhoneService.class).annotatedWith(PO.class).to(PhoneStyleOne.class);
bind(PhoneService.class).annotatedWith(PW.class).to(PhoneStyleTwo.class);

注入:
@Inject @PO PhoneService ps1;
@Inject @PW PhoneService ps2;

第二种:使用Name注解标记

绑定:
bind(PhoneService.class).annotatedWith(Names.named("phOne")).to(PhoneStyleOne.class);
bind(PhoneService.class).annotatedWith(Names.named("phTwo")).to(PhoneStyleTwo.class);
注入:
@Inject @Named("phOne") PhoneService ps1;
@Inject @Named("phTwo") PhoneService ps2;

5.实例绑定:

实例绑定,一般的将绑定一些基本类型和字符串类型,不建议将复杂类型绑定到特定的实例。
bind(Integer.class).annotatedWith(Names.named("width")).toInstance(10);
bind(String.class).annotatedWith(Names.named("name")).toInstance("zhangsan");


6.复杂类型实例的绑定:


由于RoboGuice官方不推荐使用 Instance绑定复杂类型,所以正确的方法应该是使用@ Providers,
定义:
在AbstractModule实例中,定义方法,并用@ Provides进行标注:
@Provides @Named("MyPhoneService")
PhoneService provideMyPhoneService(){
return new PhoneService() {
@Override
public void call() {
System.out.println(" Providers PhoneService");
}
};
}

@Provides @PO
PhoneService provideMyPhoneService1(){
return new PhoneService() {
@Override
public void call() {
System.out.println(" Providers PhoneService one");
}
};
}
注入:
@Inject @Named("MyPhoneService") PhoneService myservice;
@Inject @PO PhoneService ms1;

或者定义到Provider单独实现类中,这个类一定要实现roboguice的接口:

定义Provider:
public class ProviderString implements Provider<PhoneStyleTwo> {

@Override
public PhoneStyleTwo get() {
System.out.println("get PhoneStyleTwo");
return new PhoneStyleTwo();
}

}
绑定:
bind(PhoneStyleTwo.class).toProvider(ProviderString.class);
注入:
@Inject PhoneStyleTwo two;

7.单例:


RoboGuice允许在一定的范围内重用类的实例,有两个标记可以将一个类指定为单例:
@Singleton
@ContextScoped


8.默认是实现的第二种方式定义:


@ImplementedBy(Class)
public interface InterfaceOne{
void method();
}

bind(InterfaceOne.class).to(Class);等效,
如果同时存在@ ImplementedBy(Class)和bind,将优先使用bind中的实现。

@ProvidedBy(Provider.class)
public interface TransactionLog {
void method();
}
与bind(TransactionLog).toProvider(Class);
如果同时存在@ ProvidedBy(Class)和bind,将优先使用bind中的实现。


9.参数化类型的绑定:


使用TypeLiteral来创建此类型的绑定,
bind(new TypeLiteral<List<String>>(){}).toInstance(new ArrayList<String>());
或者使用@ Providers
@Provides
List<String> provideString()
{
return new LinkedList<String>();
}

10.注解总结:


@InjectExtra
@InjectPreference
@InjectResouece
@InjectView
@Inject
@SharedPreferencesName

此外,RoboGuice还提供了简单的消息publish/subscribe 机制,以及可以支持Dependency Injection的RoboThread, RoboAsyncTask ,RoboLooperThread 等,

 


11.RoboGuice对各种资源文件,及数据传递的支持:


@InjectView (R.id.styled_text) TextView styled_text; 
@InjectView (R.id.plain_text) TextView plain_text; 
@InjectView (R.id.res1) TextView res1; 
@Inject Resources res; 
@InjectResource(R.string.styled_text) String str; 
@InjectExtra ("Extra2" ) String extra2; 
@InjectExtra (value="Extra3" , optional=true) String extra3;

 

附上几张有用的图片:

  

 

  

本例练习下载地址为:http://files.cnblogs.com/Gordon-YangYiBao/roboguice.rar

0 0