是否可以将一个上下文注入到另一个上下文中?
也许这个问题不太清楚,但通过一个例子,我想我会澄清它。在我的项目中,我有两种测试:集成测试和场景测试。他们都需要一个数据源(带有定制的属性占位符)。
在集成测试用例的上下文中,仅定义数据源和占位符,如下所示:
<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>
在我的场景测试用例上下文中,我也需要这些对象(以及其他一些东西),但我只想创建一个集成上下文'在我的场景上下文中,所以是某种继承。
我已经在我的场景测试用例中使用 classPathApplicationContext
进行了尝试(一次使用lazy-init为true),如下所示:
<bean class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg>
<list>
<value>classpath:overridingTestContext.xml</value>
</list>
</constructor-arg>
</bean>
但是他没有在场景上下文。这是一个很难用谷歌搜索的问题,这就是为什么我在这里问它。我希望有人能提供解决方案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Spring 可以组合多个应用程序上下文,并通过仅允许一个具有相同 id 的 bean 来提供一种继承相似的模型。如果两个 bean 共享相同的 id,则后者将覆盖前者。
因此,您可以简单地以正确的顺序导入您需要的配置,例如,
但是该行为确实取决于
setAllowBeanDefinitionOverriding 默认为
true
。这能回答你的问题吗?
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.,
The behaviour does however depend on the value of
setAllowBeanDefinitionOverriding which defaults to
true
.Does this answer your question?