开发者

Is it possible to inject a context into another context?

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" 开发者_如何学Goclass="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.


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?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜