Android roboguice 应用
开源的roboguice是一个依赖注入框架,如果你用过Spring 应该知道其好处。
减少程序员的负担,代码变的更加简洁。
地址:https://github.com/roboguice/roboguice
工具用的是Android Studio 因为Gradle可以自动添加第三方库。
Gradle部分内容:
dependencies {
compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
compile ‘com.android.support:appcompat-v7:22.1.1‘
compile ‘org.roboguice:roboguice:2.0‘
compile (‘com.google.inject.extensions:guice-assistedinject:3.0‘){
exclude group: ‘com.google.inject‘
}
}
版本用的是2.0,官方版本最新是3.0
演示基本用法
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.google.inject.Binder;
import com.google.inject.ImplementedBy;
import com.google.inject.Inject;
import com.google.inject.Module;
import roboguice.activity.RoboActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
//会调用setContentView方法
@ContentView(R.layout.activity_main)
public class MainActivity extends RoboActivity {
@InjectView(R.id.text1)
private TextView textView; //代替findViewById 不用自己强转类型
@InjectView(R.id.btn1)
private TextView button;
@Inject
private MyCache<String,String> myCache;//单列
@Inject
private IService myService;//接口注入
@Inject
private Toaster toaster;//Toast简单的用法封装
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
textView.setText("Test is ok." + myCache);
toaster.showMsg("hey, I like to talk to you" );
myService.login("1","1");
}
});
}
}
Toaster.java
import android.app.Activity;
import android.widget.Toast;
import com.google.inject.Inject;
import roboguice.inject.ContextSingleton;
/**
* Created by gaofeng on 15-5-13.
*/
//可以获取当前Activity中的上下文
@ContextSingleton
public class Toaster {
@Inject
private Activity activity;
//不用传上下文了
public void showMsg(String msg) {//不用自己传入Context,Activity上下文对象
Toast.makeText(activity,msg, Toast.LENGTH_SHORT).show();
}
}
MyCache.java
import com.google.inject.Singleton;
import java.util.HashMap;
import java.util.Map;
/**
* Created by gaofeng on 15-5-13.
*/
//Cache通常是单列的
@Singleton
public class MyCache<K,V> { //这里只是演示,没有写太复杂
Map<K,V> map = new HashMap<K,V>();
public void put(K k,V t) {
map.put(k,t);
}
public V get(K k) {
return map.get(k);
}
}
下面是接口实例化的代码,做java ssh web开发一般都是这个样子
public interface IService { //简单业务接口
public void login(String u,String passwd);
}
import roboguice.inject.ContextSingleton;
public class MyService implements IService {
@Inject
private MyCache<String,String> myCache; //和上一次的对象是同一个,单列对象
private Context context;
public MyService() {
}
public MyService(Context context) {
this.context = context;
}
@Override
public void login(String u, String passwd) {
Log.d("", "Service Login>>>>" + context + " " + myCache);
//业务代码xxxx
}}
实现上面的接口实列化 需要配置注册
import android.content.Context;
import android.util.Log;
import com.google.inject.AbstractModule;
/**
* Created by gaofeng on 15-5-13.
*/
public class CustomModule2 extends AbstractModule {
private Context context;//系统会自己传入上下文
public CustomModule2(Context context) {
this.context = context;
}
@Override
protected void configure() {
Log.d("", "CustomModule2>>>>>>>");
bind(IService.class).toInstance(new MyService(context));//注册
}
}
还需要配置在 res/values/ 中添加文件roboguice.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="roboguice_modules">
<item>com.example.gaofeng.roboguice_demo.CustomModule2</item>
</string-array>
</resources>
掌握基本的用法就能减少大量的代码。
不用自己去new对象,单列也很好弄,不用自己定义static对象
参考: https://github.com/roboguice/roboguice/wiki
时间: 2024-12-06 19:55:42