Spring Profiles:ActiveProfilesResolver的简单示例? [英] Spring Profiles: Simple example of ActiveProfilesResolver?
问题描述
我在做什么?
我有一个要在不同环境中测试的应用程序-开发,暂存等
what am I doing?
I have an app that I want to test across different environments - dev, staging, etc
我做什么?
我正在使用maven cargo
插件来部署应用程序战争以运行集成测试.
What I do?
I am using maven cargo
plugin to deploy application war to run integration tests.
我需要什么?
我需要根据
What I need?
I need to my test to infer the spring.profiles.active
based on environment variable set by cargo like
<container>
<containerId>tomcat7x</containerId>
<systemProperties>
<spring.profiles.active>development</spring.profiles.active>
</systemProperties>
</container>
为什么?
这样我就可以删除集成测试@ActiveProfiles("development")
中的硬编码,并且该测试可以从环境变量
Why?
so that I can remove the hard coding in my Integration Test @ActiveProfiles("development")
and the test can infer what active profile is from environment variable
问题
-我发现带有配置文件的Spring集成测试,其中提到了使用
-我试图找到如何利用它,但找不到任何资源
我正在寻找有关如何使用ActiveProfilesResolver
Question
- I found Spring integration tests with profile which mentions about using ActiveProfilesResolver
- I tried to find how can I make use of it, but could not find any resources
I am looking for guidance/recommendations on how to use ActiveProfilesResolver
推荐答案
尝试一下.我从不同的地方学习并创建了以下内容.它对我有用
Try this. I learned from different places and created the following. It works for me
public class SpringActiveProfileResolver implements ActiveProfilesResolver {
@Override
public String[] resolve(final Class<?> aClass) {
final String activeProfile = System.getProperty("spring.profiles.active");
return new String[] { activeProfile == null ? "integration" : activeProfile };
}
}
然后在您的测试班上执行此操作
and on you test class do this
@ActiveProfiles(resolver = SpringActiveProfileResolver.class)
public class IT1DataSetup {
......
}
这篇关于Spring Profiles:ActiveProfilesResolver的简单示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!