2017-08-25 287 views
0

所以這種方法可能不可行,我誤解了構造。@ConditionalOnProperty由類實現的接口的內部類擴展NestedCondition

我正在使用Spring,我有一個接口,我想用於兩種不同類型的@Conditional檢查。我聲明瞭我的內部類並指定了我正在查找的@ConditonalOnProperty環境值。

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 

public interface CloudEnvironments { 
    @ConditionalOnProperty(name = "spring.profiles.active", havingValue = "dev") 
    class IsDev {} 

    @ConditionalOnProperty(name = "spring.profiles.active", havingValue = "stage") 
    class IsStage {} 

    @ConditionalOnProperty(name = "spring.profiles.active", havingValue = "prod") 
    class IsProd {} 
} 

然後我創建了我的類,它擴展了適當的Spring NestedCondition。一來檢查任何環境相匹配(雲)和一個地方的環境匹配的NONE(本地)

import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; 
import org.springframework.boot.autoconfigure.condition.NoneNestedConditions; 

public class EnvironmentConditional { 
    public static class AnyCloud extends AnyNestedCondition implements CloudEnvironments { 
     public AnyCloud() { 
      super(ConfigurationPhase.PARSE_CONFIGURATION); 
     } 
    } 

    public static class NotCloud extends NoneNestedConditions implements CloudEnvironments { 
     public NotCloud() { 
      super(ConfigurationPhase.PARSE_CONFIGURATION); 
     } 
    } 
} 

,然後我在我的自定義數據源實現它

@Configuration 
@Conditional(EnvironmentConditional.AnyCloud.class) 
public class CloudDataSource { 
    @Bean 
    public DataSource stuff(){ 
     /** CODE **/ 
    } 
} 

不幸的是,當我運行應用程序顯示所有條件匹配時

- AnyNestedCondition 0 matched 0 did not (EnvironmentConditional.AnyCloud) 

含義它沒有看到任何@ConditionalOnProperty檢查。 (如果我把它拉高一級,我就可以完成這項工作,但我很好奇,如果我能以這種方式使它更加乾燥)。

我有一個靜態的內部類實現了另一個內部類......它似乎是我已經太深了。

我是否可以在邏輯失誤時得到一些推理,以及是否有一種方法可以讓每個環境都擁有一個@ConditonalOnProperty,同時利用Spring的NestedConditions。

回答

0

我給了這樣一個嘗試,我能夠決定匹配的條件。 我們也可以簡單地利用@Profile("cloud"),使輪廓特定豆

public class EnvironmentConditional { 
    public static class AnyCloud extends SpringBootCondition implements CloudEnvironments { 


     @Override 
     public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { 

      Environment env=context.getBeanFactory().getBean(Environment.class); 
      for(String profile : env.getActiveProfiles()){ 
       switch(profile){ 

       case "dev" : return new ConditionOutcome(true, "Matched"); 
       case "prod" : return new ConditionOutcome(false, "UnMatched"); 
       } 
      } 
      return new ConditionOutcome(true, "Matched"); 
     } 
    } 

    public static class NotCloud extends NoneNestedConditions implements CloudEnvironments { 
     public NotCloud() { 
      super(ConfigurationPhase.PARSE_CONFIGURATION); 
     } 
    } 
} 

也看看這個,

@ConditionalOnCloudPlatform

Doc link

+0

這似乎是在做這我不需要CloudEnvironments接口,而是將邏輯上移到getMatchOutcome。 不幸的是,這個邏輯將在AnyCloud以及NoCloud條件檢查中被複制,這是我希望避免的。 – mcnichol

+0

好吧,只要我更新了我的答案,就可以根據環境確定任何雲或無雲。您的觀點是有效的,儘管只有一個級別 – Barath

+0

@milk也可以看看ConditionalOnCloudPlatform https://docs.spring.io/spring-boot/docs/2.0.0.BUILD-SNAPSHOT/api/org/springframework/boot/自動配置/狀態/ ConditionalOnCloudPlatform.html – Barath