Question

Maybe the question isn't that clear, but with an example, I guess I'll clear it out. In my project, I have two kinds of tests: integration tests and scenario tests. They both need a datasource (with a custom made propertyplaceholder).

In the context of the integration testcase, in only define the datasource and the placeholder, like this:

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${db.driver}" />
    <property name="url" value="${db.url}" />
    <property name="username" value="${db.username}" />
    <property name="password" value="${db.password}" />
</bean>

<bean id="propertyResolver" class="com.cegeka.bibliothouris.test.MultiThreadedPropertyResolver">
    <property name="location"><value>classpath:testContext.properties</value></property>
</bean>

In my scenario testcase context, I need these objects as well (together with some other stuff), but I just want to create an integration context 'in' my scenario context, so some kind of inheritance.

I've already tried it with a classPathApplicationContext in my scenario testcase (once with lazy-init on true),like this:

<bean class="org.springframework.context.support.ClassPathXmlApplicationContext">
    <constructor-arg>
        <list>
            <value>classpath:overridingTestContext.xml</value>
        </list>
    </constructor-arg>
</bean>

But he doesn't create a dataSource in the scenario context. This is a problem very hard to google, that's why I'm asking it here. I hope someone has the solution.

Was it helpful?

Solution

Spring can combine several appicationcontexts and provides an inheritance-similar model by allowing only one bean with the same id. If two beans share the same id the latter will override the former.

Hence you can simply import the configs you need in the proper order using e.g.,

<import resource="context.xml" />

The behaviour does however depend on the value of setAllowBeanDefinitionOverriding which defaults to true.

Does this answer your question?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top