0

I am using androidannotations in an Android project and I have an EBean defined as:

@EBean(scope = Singleton)
class MyBean {
    public void someFunction() {
    }
}

This bean is created whenever it is first required, that is, whenever I have:

@Bean
MyBean myBean;

in some class. I am also using EventBus in this project, and a function in MyBean is a @Subscriber. This function is not called (when an Event is posted) because an instance of MyBean does not exist (since I don't have the dependency anywhere). How can I create an instance of MyBean without specifically adding the dependency anywhere?

My temporary solution is to simply define the bean in my Application class.

4

2 回答 2

0

我认为您正在以最干净的方式进行操作。让您的 CustomApplication 类包含 @Bean MyBean myBean,即使它从不调用其上的函数。对于 AA 和事件总线订阅者类,这是我找到的唯一解决方案。

该类需要在某处实例化。由于它是一个单例,在 CustomApplication 类中执行它似乎是最干净的。另一种方法可能是将类设为服务(在 Android 意义上 - “扩展服务”),这将允许您在 AndroidManifest.xml 文件中声明它。但由于您不需要任何 Android 服务功能,这似乎是错误的做法。

于 2016-02-24T19:41:30.507 回答
0

EBean事实证明,如果不将其注入某处,就无法创建。参考这个问题:https ://github.com/excilys/androidannotations/issues/1692

我的解决方案是有一个类EBeanManager(也是一个EBean),它只是注入我需要创建的 bean。EBeanManager被注入我的Application课堂。我在单独的课堂上做这件事的原因是为了避免课堂上的混乱Application

于 2016-02-25T10:49:13.060 回答