如何使用@IfProfileValue测试配置文件是否处于活动状态? [英] How can I use @IfProfileValue to test if a Profile is active?

查看:240
本文介绍了如何使用@IfProfileValue测试配置文件是否处于活动状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

令人困惑 @IfProfileValue @Profile @ActiveProfiles <无关/ code>。 @Profile 测试一个配置文件是否处于活动状态, @ActiveProfiles 将它们设置为活动状态, @IfProfileValue 允许您检查Spring Environment 中的内容。武汉理工大学?我弃用了所有这些并添加了新的 @IfEnvironment @IfProfile @ActivateProfiles

So confusingly @IfProfileValue has nothing to do with @Profile or @ActiveProfiles. @Profile tests to see if a profile is active, @ActiveProfiles sets them as active, and @IfProfileValue allows you to check things in Spring Environment. Wut? I'd deprecate all of them and add new ones @IfEnvironment, @IfProfile, and @ActivateProfiles.

除了评论之外,如何使用 @IfProfileValue 来检测我的个人资料是否有效?我目前没有在这个项目上使用Spring Boot。答案应该显示代码,如果配置文件被激活为 @ActiveProfiles(test),我们将假设我希望测试运行。

Commentary aside, how can I use @IfProfileValue to detect whether i have a profile active? I am not using Spring Boot on this project, at this time. Answers should show code, and we will assume that I want the test to run if the profile is activated as @ActiveProfiles( "test" ).

我试过 @IfProfileValue(name =activeProfiles,value =test)但这似乎跳过了测试,这意味着它是不匹配。我将推测问题可能与 ActiveProfiles 是一个集合这一事实有关。

I tried @IfProfileValue(name = "activeProfiles", value = "test") but that seems to have the test skipped, which means it's not matching. I'm going to speculate the problem may have to do with the fact that ActiveProfiles is a Collection.

推荐答案


如此混乱 @IfProfileValue 无关使用 @Profile
@ActiveProfiles

这是正确的,我在这里详细解释了这一点: https://stackoverflow.com/a / 23627479/388980

That's correct, and I explained this in detail here: https://stackoverflow.com/a/23627479/388980

...我假设你已经看过了,因为你昨天对我的回答发表了评论。

... which I'm assuming you have already seen, since you commented on my answer yesterday.

@IfProfileValue 的原因与 @Profile 或<$无关c $ c> @ActiveProfiles 是由于框架的演变。有关详细信息,请参阅下文。

The reason that @IfProfileValue has nothing to do with @Profile or @ActiveProfiles is due to the evolution of the framework. See below for further details.


@Profile 测试以查看配置文件是否为有效,
@ActiveProfiles 将它们设置为有效, @IfProfileValue 允许
你检查春天的事情环境

@Profile tests to see if a profile is active, @ActiveProfiles sets them as active, and @IfProfileValue allows you to check things in Spring Environment.

这些陈述并不完全正确,尤其是最后一部分。

These statements are not entirely correct, especially the last part.

@Profile 用于有选择地启用组件(例如, @服务等), @Configuration 类,或 @Bean 方法,如果其中一个 bean定义配置文件 ApplicationContext 的Spring 环境中处于活动状态。此注释与测试没有直接关系: @Profile 应该在测试类上使用。

@Profile is used to selectively enable a component (e.g., @Service, etc.), @Configuration class, or @Bean method if one of the named bean definition profiles is active in the Spring Environment for the ApplicationContext. This annotation is not directly related to testing: @Profile should not be used on a test class.

@ActiveProfiles 用于指定哪些 bean定义配置文件(例如,通过 @Profile )在加载 ApplicationContext 进行集成测试时应该处于活动状态。

@ActiveProfiles is used to designate which bean definition profiles (e.g., those declared via @Profile) should be active when loading an ApplicationContext for an integration test.

@IfProfileValue 允许您检查Spring 环境中的内容。我不确定你为什么这么做,因为Spring Framework中没有任何文档说明这一点。正如我在上述主题中所述:

@IfProfileValue does not allow you to check things in the Spring Environment. I'm not sure why you are assuming this, since none of the documentation in the Spring Framework states that. As I stated in the aforementioned thread:

请注意,Spring Framework 2.0中引入了 @IfProfileValue 在bean定义配置文件的概念之前, long ,并且在Spring Framework 3.1中首次引入了 @ActiveProfiles

Please note that @IfProfileValue was introduced in Spring Framework 2.0, long before the notion of bean definition profiles, and @ActiveProfiles was first introduced in Spring Framework 3.1.

在前面提到的帖子中,我还指出了以下内容:

In the aforementioned thread, I also pointed out the following:

在考虑时,个人资料一词可能会产生误导 @IfProfileValue 的语义。关键是要考虑测试组(如TestNG中的测试组)而不是配置文件。请参阅 JavaDoc for @IfProfileValue

The term 'profile' is perhaps misleading when considering the semantics for @IfProfileValue. The key is to think about 'test groups' (like those in TestNG) instead of 'profiles'. See the examples in the JavaDoc for @IfProfileValue.


我如何使用 @IfProfileValue 检测我的个人资料是否有效?

how can I use @IfProfileValue to detect whether i have a profile active?

这取决于......我当你说个人资料时,我假设你的意思是 bean定义个人资料

That depends, and... I'm assuming you mean bean definition profile when you say "profile".

如果你正在使用 @ActiveProfiles 为您的测试设置 bean定义配置文件,您目前无法使用 @IfProfileValue 来确定是否 bean定义配置文件处于活动状态,因为通过 @ActiveProfiles 配置的 bean定义配置文件直接在测试的 ApplicationContext 而不是Java系统属性。

If you're using @ActiveProfiles to set the bean definition profiles for your tests, you cannot currently use @IfProfileValue to determine if a bean definition profile is active, since the bean definition profiles configured via @ActiveProfiles are set directly in the test's ApplicationContext and not as a Java system property.

但是,如果要设置 bean定义配置文件 <强大>仅通过 spring.profiles.active 系统属性,然后可以使用 @ IfProfileValue 确定 bean定义配置文件是否处于活动状态,因为 @IfProfileValue 实际上适用于系统属性。例如,您可以使用以下内容:

However, if you are setting the bean definition profiles only via the spring.profiles.active system property, then it would be possible to use @IfProfileValue to determine if a bean definition profile is active, since @IfProfileValue in fact works with system properties. For example, you could then use the following:

@IfProfileValue(name = "spring.profiles.active", value = "test")




我试过 @IfProfileValue(name =activeProfiles,value =test)
似乎跳过了测试,这意味着它不匹配。

I tried @IfProfileValue(name = "activeProfiles", value = "test") but that seems to have the test skipped, which means it's not matching.

由于 activeProfiles 是不正确的属性名称,因此无效。正确的系统属性名称是 spring.profiles.active 。有关详细信息,请参阅 AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME

That won't work since activeProfiles is the incorrect property name. The correct system property name is spring.profiles.active. See AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME for details.

事实 @IfProfileValue 不一致@ActiveProfiles 是Spring团队的一个已知问题。如果您愿意,请参阅以下JIRA问题并加入讨论。

The fact that @IfProfileValue does not work in harmony with @ActiveProfiles is a known issue to the Spring team. Please consult the following JIRA issues for further details and to join in on the discussions if you'd like.

  • https://jira.spring.io/browse/SPR-7754
  • https://jira.spring.io/browse/SPR-8982
  • https://jira.spring.io/browse/SPR-11677

希望这能为您澄清情况!

Hope this clarifies the situation for you!

Sam( Spring TestContext Framework的作者

这篇关于如何使用@IfProfileValue测试配置文件是否处于活动状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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