如何控制在测试环境和生产环境之间变化的弹簧注入? [英] How do I control spring injections that vary between the test environment and the production environment?

查看:12
本文介绍了如何控制在测试环境和生产环境之间变化的弹簧注入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个 CI 情况,我将在其中将我的 Web 应用程序部署到测试环境.在这个测试环境中,我希望应用程序使用的业务对象是真实对象的模拟;模拟将返回静态测试数据.我正在使用它对我的 ui 运行测试.我正在使用 Spring 控制这些业务对象依赖项的注入;这是一个 struts 2 应用程序,值得.

I'm setting up a CI situation in which I will deploy my web app to a test environment. In this test environment, I want the business objects used by the app to be mocks of the real ones; the mocks will return static test data. I'm using this to run tests agains my ui. I'm controlling the injections of these business object dependencies with Spring; it's a struts 2 application, for what that's worth.

我认为我的问题与 Maven 相关.让我的 Maven 构建确定是否构建弹簧配置以注入模拟或注入真实事物的最佳方法是什么?这对 Maven 配置文件有用吗?其他选择?

My question is Maven related, I think. What is the best way to have my Maven build determine whether or not to build the spring configuration out for injecting the mocks or injecting the real thing? Is this a good use for maven profiles? Other alternatives?

推荐答案

对构建 Web 应用程序工件 (Maven 在 CI/Hudson 支持下为多个环境 [prod、test、dev] 生成工件的最佳实践?).虽然您可以使用各种机制为不同的上下文生成具有不同 Spring 注入配置的 WAR 文件,但每次构建 WAR 工件时都应该是相同的.

It's probably a bad idea to do anything with the build of the web app artifact ( Maven best practice for generating artifacts for multiple environments [prod, test, dev] with CI/Hudson support? ). While you could use various mechanisms to produce a WAR file with different configurations of the Spring injections for different contexts, the WAR artifact should be the same every time it's built.

为了从 WAR 中提取配置,我使用了 Spring 3 从外部属性文件中提取覆盖值的能力.我定义了我的业务对象的默认值,即生产值.我配置 spring 来检查是否存在属性文件,当应用程序处于测试环境并需要模拟注入时,我将部署该文件.如果该属性文件存在,则将其值注入.这是 spring 配置文件的相关位.

In order to extract the configuration out of the WAR, I have used Spring 3's ability to pull in override values from an external property file. I define default, i.e. produciton, values of my business objects. And I configure spring to check for the existence of a properties file, something I will deploy when the app is in a testing environment and requires mock injections. If that properties file exists, it's values are injected instead. Here's the relevent bit of the spring config file.

<!-- These are the default values -->
    <util:properties id="defaultBeanClasses">
    <prop key="myManagerA">com.myco.ManagerAImpl</prop>
    <prop key="myManagerB">com.myco.ManagerBImpl</prop>
</util:properties>

<!-- Pull in the mock overrides if they exist. -->
<context:property-placeholder 
    location="file:///my/location/mockBeans.properties"
    ignore-resource-not-found="true"
    properties-ref="defaultBeanClasses"/>

<!-- The beans themselves. -->  
<bean id="managerA" class="${myManagerA}"/>
<bean id="managerB" class="${myManagerB}"/>

这是外部mockBeans.properties"文件的内容:

And here is the contents of the external "mockBeans.properties" file:

#Define mock implementations for core managers
myManagerA=com.myco.ManagerAMockImpl
myManagerB=com.myco.ManagerBMockImpl

这很好用.如果您愿意,您甚至可以将 mockBeans.properties 文件包含在实际的 WAR 中,但不能包含在实时位置中.那么测试环境任务也会将其移动到spring config指向的位置.或者,您可以将模拟属性驻留在完全不同的项目中.

This works nicely. You can even include the mockBeans.properties file in the actual WAR, if you like, but not in the live location. Then the test environment task would be too move it to the location pointed at by the spring config. Alternatively, you could have the mock properties reside in a completely different project.

这篇关于如何控制在测试环境和生产环境之间变化的弹簧注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆