开发者

Moving Spring Dependency Injected code to Android

Aloha -

I'm migrating code from an existing web application into an Android application. The existing web app uses Spring extensively for dependency injection.

I'd like to reuse the Spring dependency injection if I can, but I do not see a way to initialize it like you would in a webapp:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
开发者_运维知识库

Google does not turn up much help on the subject, other than use Guice. I'd rather not do that as the web app is still being developed, so if I were to use Guice for dependencies, I would constantly need to sync up between the Spring dependencies and Guice.

Is there any way to use Spring for DI on Android in a plug and play fashion, or do I have to look at rewriting all this stuff in another way?

Thanks!


Do not try to use spring dependency injection in your android app. Android and spring do not mix together in any way,shape, or form. Instead, use the factory design pattern to generate objects.


Have you looked at rolling your on ApplicationContext, perhaps extending FileSystemXmlApplicationContext and overriding getResourceByPath(String path) to pull your context files from the assets/ folder?

If you decide to go down the Guice route, RoboGuice is a nice little library: http://code.google.com/p/roboguice/


Please DO use Spring dependency injection in your android app! :-)

However you won't be able to use the original Spring Framework, as it requires the java.beans package, that is not present on Android.

If your existing Spring configuration is not too extraordinary, you will be happy with my port of the Spring Framework called RoboSpring. From its description:

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:

  • 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

Please see here: https://github.com/dthommes/RoboSpring

Moving Spring Dependency Injected code to Android

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜