【问题标题】:Spring Profiles: Simple example of ActiveProfilesResolver?Spring Profiles:ActiveProfilesResolver 的简单示例?
【发布时间】:2014-05-26 13:26:42
【问题描述】:

我在做什么?
我有一个应用程序,我想在不同的环境中进行测试 - 开发、登台等

我该怎么做?
我正在使用maven cargo 插件部署应用程序战争以运行集成测试。

我需要什么?
我需要根据货物设置的环境变量来推断spring.profiles.active,例如

                    <container>
                        <containerId>tomcat7x</containerId>
                        <systemProperties>
                            <spring.profiles.active>development</spring.profiles.active>
                        </systemProperties>
                    </container>

为什么?
这样我就可以删除集成测试@ActiveProfiles("development") 中的硬编码,并且测试可以从环境变量中推断出活动配置文件是什么

问题
- 我找到了Spring integration tests with profile,其中提到了使用ActiveProfilesResolver
- 我试图找到如何使用它,但找不到任何资源
我正在寻找有关如何使用ActiveProfilesResolver的指导/建议

【问题讨论】:

    标签: java spring maven


    【解决方案1】:

    试试这个。我从不同的地方学习并创建了以下内容。它对我有用

    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 };
        }
    }
    

    在你的测试课上这样做

    @ActiveProfiles(resolver = SpringActiveProfileResolver.class)
    public class IT1DataSetup {
    ......
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 2014-10-30
      • 2014-07-12
      • 2017-11-26
      • 1970-01-01
      • 2012-03-13
      • 2014-09-18
      相关资源
      最近更新 更多