【FastDev4Android框架开发】AndroidAnnnotations注入框架使用之第三方框架集成RoboGuice(十三)

转载请标明出处: 

http://blog.csdn.net/developer_jiangqq/article/details/49530307
本文出自:【江清清的博客】

().前言:   

      前面我们已经对于AndroidAnnotations框架的线程处理做了讲解,今天我们开始具体学习一下第三方框架集成。

    FastDev4Android框架项目地址:https://github.com/jiangqqlmj/FastDev4Android 

().RoboGuice集成

2.1.简介说明

AndroidAnnotations1.0,AndroidAnnotations已经完成集成了RoboGuice1.1.1

AndroidAnnotations3.1,发布的RoboGuice1.0版本已经从RoboGuice2.0版本中移除了。

AndroidAnnotations3.3,从发布的RoboGuice3.0版本开始,该更新已经被支持。现在RoboGuice的最新版本是3.0


 


 

2.2.集成RoboGuiceAndroidAnnotations

@EActivity(R.layout.main)
@RoboGuice({AstroListener.class,AnotherListener.class})
public classAstroGirl extends Activity {
 
    @ViewById
    EditText edit;
 
    @Inject
    GreetingService greetingService;
 
    @Click
    void button() {
        String name =edit.getText().toString();
        greetingService.greet(name);
    }
}
 
public classAstroListener {
 
        @Inject
        Context context;
 
        public voiddoSomethingOnResume(@Observes OnResumeEvent onResume) {
                Toast.makeText(context,"Activity has been resumed", Toast.LENGTH_LONG).show();
        }
}
2.3. RoboGuice 优点

RoboGuice对于Android应用程序有GoogleGuice框架的支持,这意味着我们可以从依赖注入获得更加的优势,高内聚,低耦合。

2.4.AndroidAnnotations优点

  • Android编译的时候进行注入组件(没有性能的影响)
  • 我们不需要在继承RoboActivity类了,这意味中我们可以集成任何类了,哪怕这些类不支持RoboGuice。这样我们就可以更多思考如何扩展我们的基类。
  • 支持@Click,@Background,@UiThread,@ItemSelect等等注解。

2.5.@EBean中使用RoboGuice

        AndroidAnnotations2.4

       假设我们现在有一个@EBean注解的组件,现在我们想用RoboGuice来注入一些组件。使用@EActivity注解的Activity允许使用RoboGuice注入。不过现在RoboGuice现在仅支持Activity。从AndroidAnnotations官方发布的消息是:现在他们没有花很多精力在RoboGuice上面,因为如果要全面支持RoboGuice,那么现在项目需要改动很多地方了。

       但是这也不意味着我们不能在@EBean注解的类中使用RoboGuice,你只需要手动进行Guice注入,就和RoboActivity达到一样的效果啦。使用实例如下:

@EBean
public class MyBean{
 
    @App
    MyRoboApplication application;
 
    @Inject
    SomeClass myRoboGuiceDependency;
 
    @AfterInject
    void injectRoboGuiceDependencies() {
       application.getInjector().injectMembers(this);
    }
 
} 
       

          到此位置关于AndroidAnnotations第三方框架集成之RoboGuice集成已经全部讲解完成了。

同时FastDev4Android项目已经添加配置了AndroidAnnotations框架,后期的框架项目中也会主要使用这个DI框架,.欢迎大家去Github站点进行clone或者下载浏览:https://github.com/jiangqqlmj/FastDev4Android 同时欢迎大家star和fork整个开源快速开发框架项目~



  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值