Brian Teeter
Brian Teeter

Reputation: 876

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!

Upvotes: 2

Views: 798

Answers (3)

Dr. Daniel Thommes
Dr. Daniel Thommes

Reputation: 697

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

RoboSpring - Inversion of Control for Android

Upvotes: 0

Brian Griffey
Brian Griffey

Reputation: 4751

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.

Upvotes: 2

djg
djg

Reputation: 1283

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/

Upvotes: 1

Related Questions