0

I'm want to use RoboSpring as inversion of control inside my Android application. Application consists of Android library and separate project which overrides some resources from library. And there are a lot of other projects which use this library. So library is like a basement and projects role is to change logo, theme and so on, but not a logic of application. But now I need to change some logic inside one of the applications. So I decided to use RoboSpring.

I'm created inside library's src folder file applicationContext.xml with

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/util
           http://www.springframework.org/schema/util/spring-util.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/context 
           http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:annotation-config />

    <bean id="myServiceUrl" class="java.net.URL">
        <constructor-arg value="http://www.robospring.org" />
    </bean>

    <bean id="client" class="com.example.Client">
        <constructor-arg ref="clientParser"/>
    </bean>

    <bean id="clientParser" class="com.example.Parser"/>

</beans>

As example I declared some beans and I need to change clientParser inside one of the projects.

Problem #1:
I can't even override file applicationContext.xml inside project because I'll get error: Error generating final archive: Found duplicate file for APK: applicationContext.xml
I should keep applicationContext.xml only inside project or inside library, but it's not good to copypaste this file in every project. I think it can be solved by using custom contextConfigLocation but not sure how.

Problem #2
Even if I succeded in overriding context.xml how could I override only part of beans, actually only one bean? Because I think files are not merging and I should override whole xml file.

Actually I'm new to inversion of control and maybe I'm chose not correct tool to solve my problem, so any help will be appreciated

cooperok
  • 4,249
  • 3
  • 30
  • 48

1 Answers1

1

This is a Spring related problem and you can solve it with RoboSpring as you would do it with the Spring Framework. Please have a look at this answer:

https://stackoverflow.com/a/11834159/1210793

I propose using another name for your context definition in the library project, e.g. library-context.xml. Than you can import it from your applicationContext.xml in every related project like this:

<!-- import from the classpath of your library -->
<import resource="classpath:library-context.xml"/>

<!-- overriding the clientParser bean -->
<bean id="clientParser" class="another.Parser"/>

You can also take this approach when working with Android's Build Variants (e.g. Product Flavors) by placing Spring configurations in different source folders.

Happy Overriding!

Community
  • 1
  • 1
  • Thank you for answer. I did it this way: first check bean inside project and if it's not present, check inside library. It gave me ability to override only certain beans try { RoboSpring.autowire(bean, PROJECT_CONTEXT_XML); } catch (RuntimeException e) { RoboSpring.autowire(bean, LIBRARY_CONTEXT_XML); } – cooperok Mar 25 '15 at 08:51
  • @Daniel Thommes - I want to use RoboSpring but I don't know where to put the applicationContext.xml file. I'm getting errors that it doesn't exist. I'm using Android Studio and in project view I have this file in the root of my src folder. – Greyshack Aug 28 '15 at 22:41
  • Try putting it into src/main/resources – Dr. Daniel Thommes Oct 05 '15 at 19:03