简体   繁体   English

Android注释-注入超类类型的列表

[英]Android Annotations - Injecting a list of superclass type

I am trying to achieve the following Spring code using Android Annotations: 我正在尝试使用Android注释实现以下Spring代码:

@Autowired
public initHandlerList(List<Handler> handlerList) {
   // Do stuff with the list ...
}

I tried using both an interface and a class. 我尝试同时使用接口和类。

Bean definition: Bean定义:

@EBean
public AbstractHandler implements Handler {}

Trying to inject: 尝试注入:

@Bean
public initHandlersList(List<AbstractHandler> handlersList) {
  // Do stuff with the list ...
}

But always got the following error: 但是总会出现以下错误:

Error:(20, 5) error: org.androidannotations.annotations.Bean can only be used on an element annotated with @org.androidannotations.annotations.EBean

So I guess since the list itself is not annotated with @EBean it can't be used as a Bean... any way to implement this using Android Annotations? 因此,我想既然列表本身未使用@EBean注释,则不能将其用作Bean ...使用Android注释实现此方法的任何方法吗?

Thanks ! 谢谢 !

Sorry I can't comment but my reputation is too low. 抱歉,我无法发表评论,但是我的声誉太低了。

I read the wiki and under method based injection I saw how you inject the beans. 我阅读了Wiki,并在基于方法的注入下看到了如何注入Bean。 What I can see in your code is that you are indeed creating a EBean with an AbstractHandler object however you are trying to inject a List object which has not been annotated with @EBean you can either delete the List<> and just use the AbstractHandler or you can extend a List implementation (Like ArrayList) and annotate it with @EBean. 我在您的代码中看到的是,您确实正在使用AbstractHandler对象创建一个EBean,但是您尝试注入一个未使用@EBean注释的List对象,您可以删除List <>并仅使用AbstractHandler或您可以扩展List实现(如ArrayList)并使用@EBean对其进行注释。

@EBean
public class InjectableArrayList<T> extends ArrayList<T>{}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM