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

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

问题描述

我设置了一个CI情境,其中我将我的Web应用程序部署到测试环境。在这个测试环境中,我想让应用程序使用的业务对象是真实的; mock将返回静态测试数据。我使用这个运行测试agains我的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应用程序构件的构建(用于为多个环境生成工件的最佳实践[prod,test,dev] / Hudson支持?)。虽然您可以使用各种机制来为不同上下文创建具有不同配置的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

这很好用。你甚至可以在实际的WAR中包括mockBeans.properties文件,如果你喜欢,但不是在实时位置。然后测试环境任务将太快地移动到由spring配置指向的位置。或者,您可以将模拟属性驻留在完全不同的项目中。

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天全站免登陆