简体   繁体   English

Spring Profile - 如何包含AND条件以添加2个配置文件?

[英]Spring Profile - How to include AND condition for adding 2 profiles?

I don't want a specific bean to be loaded if either the active profile is test or local. 如果活动配置文件是test或local,我不希望加载特定的bean。 However when I set the below , spring seems to be treating is as "or" and the method gets executed both when active profile is test or local. 但是,当我设置下面的内容时,spring似乎处理为“或”,并且当活动配置文件为test或local时,该方法都会被执行。 However if remove say local and keep test , then the bean is not created when the profile is test. 但是,如果删除说本地并继续测试,那么在配置文件测试时不会创建bean。

@Profile({"!test","!local"})

Since Spring 5.1 (incorporated in Spring Boot 2.1) it is possible to use a profile expression inside profile string annotation. 从Spring 5.1(包含在Spring Boot 2.1中)开始,可以在配置文件字符串注释中使用配置文件表达式。 So: 所以:

In Spring 5.1 (Spring Boot 2.1) and above it is as easy as: Spring 5.1(Spring Boot 2.1)及以上版本中,它很简单:

@Component
@Profile("!test & !local")
public class MyComponent {}

Spring 4.x and 5.0.x : Spring 4.x和5.0.x

There are many approaches for this Spring versions, each one of them has its pro's and con's. 这个Spring版本有很多种方法,每种方法都有它的专业版和内容版。 When there aren't many combinations to cover I personally like the approach of using @Conditional like @Stanislav answered in a similar question. 当没有很多组合要覆盖时,我个人喜欢使用@Conditional的方法,像@Stanislav在类似的问题中回答。 I post the solution here for the convenience of the reader: 为了方便读者,我在这里发布解决方案:

Your Spring Bean: 你的春豆:

@Component
@Conditional(value = { NotTestNorLocalProfiles.class })
public class MyComponent {}

NotTestNorLocalProfiles implementation: NotTestNorLocalProfiles实现:

public class NotTestNorLocalProfiles implements Condition {
    @Override
    public boolean matches(final ConditionContext context, final AnnotatedTypeMetadata metadata) {
        return !context.getEnvironment().acceptsProfiles("test")
                    && !context.getEnvironment().acceptsProfiles("local");
    }
}

Other approaches can be found in this similar questions: 其他方法可以在这个类似的问题中找到:

How to conditionally declare Bean when multiple profiles are not active? 如何在多个配置文件不活动时有条件地声明Bean?

Spring: How to do AND in Profiles? Spring:如何在Profiles中做AND?

Use @Profile({"Dummy"}) . 使用@Profile({"Dummy"})

You can replace "Dummy", if you want to use a valid profile name later . 如果您想稍后使用有效的配置文件名称 ,可以替换“Dummy”。

When using Spring Boot 1.X, you can update the System Property with an additional profile name and handle all the boolean logic before your SpringApplication.run . 使用Spring Boot 1.X时,可以使用其他配置文件名更新系统属性,并在SpringApplication.run之前处理所有布尔逻辑。 If it qualifies, you would append the profile name to the active profiles. 如果符合条件,则会将配置文件名称附加到活动配置文件。 If it doesn't, you would not change anything. 如果没有,你就不会改变任何东西。

This is a very basic check but you can add more rigorous profile validation, such as null check and actually splitting the profiles list. 这是一个非常基本的检查,但您可以添加更严格的配置文件验证,例如空检查和实际拆分配置文件列表。

    String profile = System.getProperty("spring.profiles.active");
    if(!profile.contains("test") && !profile.contains("local")) {
        System.setProperty("spring.profiles.active", profile + ",notdev");
    }

If you are using Spring Boot 2.0, you can use addAdditionalProfiles to add the profile (don't have a SB2 app so I can't test this but I assume it just adds it to the System properties list). 如果您使用的是Spring Boot 2.0,则可以使用addAdditionalProfiles添加配置文件(没有SB2应用程序,因此我无法对此进行测试,但我认为它只是将其添加到系统属性列表中)。

    String profile = System.getProperty("spring.profiles.active");
    if(!profile.contains("test") && !profile.contains("local")) {
        SpringApplication.addAdditionalProfiles("notdev");
    }

Then your annotation can be: 然后你的注释可以是:

@Profile({"notdev"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM