Android 和依赖注入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2135378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android and Dependency Injection
提问by Richard Szalay
I've been looking around, in vain, for some information on using a dependency injection container in Android development. Specifically, how to override the creation of an Activity
in a way that will also work when coming back from being killed (for whatever reason).
我一直在四处寻找有关在 Android 开发中使用依赖项注入容器的一些信息,但徒劳无功。具体来说,如何以一种Activity
在从被杀(无论出于何种原因)回来时也能工作的方式覆盖创建。
Has anyone got any experience in this area?
有没有人有这方面的经验?
采纳答案by Jay Askren
It appears you can use Google Guice 2.0with Android. You might also look into roboguice.
看来您可以在 Android 上使用Google Guice 2.0。你也可以看看roboguice。
Edit:
编辑:
Spring is also now available for Android
Spring 现在也可用于 Android
Edit:Roboguice is now deprecated. You might try Toothpickas mentioned in the comments by the developer behind Toothpick.
编辑:Roboguice 现在已弃用。您可以按照Toothpick背后的开发人员的评论中所述尝试Toothpick。
回答by Wilfred Springer
You might also want to consider Spring ME. Although originally intended for Java ME, I have seen reports from people using it for Android as well. The benefit would be that you have a familiar programming model (Spring) without the penalty: Spring ME has a 0k footprint.
您可能还想考虑Spring ME。虽然最初是为 Java ME 设计的,但我也看到有人将它用于 Android 的报告。好处是你有一个熟悉的编程模型 (Spring) 没有惩罚:Spring ME 有 0k 占用空间。
回答by Dr. Daniel Thommes
If you are used to use Spring in other projects, you won't be very happy with Spring for Android(it's only a REST library with Auth support), Spring ME(completely different workflow and reduced featureset) or Tiny Spring(e.g. only Spring-likeconfiguration).
如果你习惯在其他项目中使用 Spring,你不会对Spring for Android(它只是一个支持 Auth 的 REST 库)、Spring ME(完全不同的工作流和减少的功能集)或Tiny Spring(例如只有 Spring )感到非常满意-像配置)。
You could however give my project RoboSpringa try. From the description:
但是,您可以尝试一下我的项目RoboSpring。从描述来看:
RoboSpring is a (real) port of the Spring Framework to the Android platform. Additionally it offers preliminary support for functionality introduced by RoboGuice like injecting View references into Activities and more. RoboSpring is based on version 3.1.0 RELEASE of Spring's core, beans, context and aop components. It offers the following functionality:
RoboSpring 是 Spring 框架到 Android 平台的(真实)端口。此外,它还为 RoboGuice 引入的功能提供了初步支持,例如将 View 引用注入到活动中等等。RoboSpring 基于 Spring 的核心、bean、上下文和 aop 组件的 3.1.0 RELEASE 版本。它提供以下功能:
- Configure application components with a Spring configuration file (XML)
- Autowire your Android components with beans from the Spring application context.
- Inject the Android application context into your Spring Beans.
- Inject views into Activities.
- … and more
- 使用 Spring 配置文件 (XML) 配置应用程序组件
- 使用 Spring 应用程序上下文中的 bean 自动装配您的 Android 组件。
- 将 Android 应用程序上下文注入您的 Spring Beans。
- 将视图注入到活动中。
- … 和更多
回答by brian.hgt
There is also a new Spring project for Android: Tiny Spring. It solves the very basics of XML configuration but doesn't do everything that Spring does.
Android 还有一个新的 Spring 项目:Tiny Spring。它解决了 XML 配置的基本问题,但没有完成 Spring 所做的一切。