2016-10-04 16 views

cevap

14

Sen Bahar Boot gelen Bahar veya @ConditionalOnProperty den @Conditional kullanabilirsiniz.

  • 1.Using Spring4 (sadece),

    Eğer DEĞİL bahar çizme kullanıyorsanız, bu overkill olabilir.

Birincisi, ConditionContext Çevre erişimi sahip olduğu bir durumu sınıfını oluşturmak: O açıklama da fasulye

public class MyCondition implements Condition { 
    @Override 
    public boolean matches(ConditionContext context, 
          AnnotatedTypeMetadata metadata) { 
     Environment env = context.getEnvironment(); 
     return null != env 
       && "true".equals(env.getProperty("server.host")); 
    } 
} 

:

@Bean 
@Conditional(MyCondition.class) 
public ObservationWebSocketClient observationWebSocketClient(){ 
    //return bean 
} 
  • 2.Using Bahar Önyükleme:

@ConditionalOnProperty(name="server.host", havingValue="localhost")

Ve abcd.properties dosyasında,

server.host=localhost 
0

Böyle bir şey için bir pasaj var. Bu ek açıklamada ayarlanmış bir özelliğin değerini kontrol eder, bu nedenle Hatta aynı adla farklı fasulye tanımlamanızı sağlar

@ConditionalOnProperty(value="usenew", on=false, propertiesBeanName="myprops") 
@Service("service") 
public class oldService implements ServiceFunction{ 
    // some old implementation of the service function. 
} 

gibi şeyler kullanabilirsiniz:

@ConditionalOnProperty(value="usenew", on=true, propertiesBeanName="myprops") 
@Service("service") 
public class newService implements ServiceFunction{ 
    // some new implementation of the service function. 
} 

Bu iki kutu kendisi için ... Eğer mülkiyet açık veya kapalı olmasına bağlı olarak farklı uygulamalar ile "service" adında fasulye var sağlayan aynı anda

pasajı ilan edilmesi:

/** 
* Components annotated with ConditionalOnProperty will be registered in the spring context depending on the value of a 
* property defined in the propertiesBeanName properties Bean. 
*/ 

@Target({ ElementType.TYPE, ElementType.METHOD }) 
@Retention(RetentionPolicy.RUNTIME) 
@Conditional(OnPropertyCondition.class) 
public @interface ConditionalOnProperty { 
    /** 
    * The name of the property. If not found, it will evaluate to false. 
    */ 
    String value(); 
    /** 
    * if the properties value should be true (default) or false 
    */ 
    boolean on() default true; 
    /** 
    * Name of the bean containing the properties. 
    */ 
    String propertiesBeanName(); 
} 

/** 
* Condition that matches on the value of a property. 
* 
* @see ConditionalOnProperty 
*/ 
class OnPropertyCondition implements ConfigurationCondition { 
    private static final Logger LOG = LoggerFactory.getLogger(OnPropertyCondition.class); 

    @Override 
    public boolean matches(final ConditionContext context, final AnnotatedTypeMetadata metadata) { 
     final Map attributes = metadata.getAnnotationAttributes(ConditionalOnProperty.class.getName()); 
     final String propertyName = (String) attributes.get("value"); 
     final String propertiesBeanName = (String) attributes.get("propertiesBeanName"); 
     final boolean propertyDesiredValue = (boolean) attributes.get("on"); 

     // for some reason, we cannot use the environment here, hence we get the actual properties bean instead. 
     Properties props = context.getBeanFactory().getBean(propertiesBeanName, Properties.class); 
     final boolean propValue = parseBoolean(props.getProperty(propertyName, Boolean.toString(false))); 
     LOG.info("Property '{}' resolved to {}, desired: {}", new Object[] { propertyName, propValue, "" + propertyDesiredValue }); 
     return propValue == propertyDesiredValue; 
    } 
    /** 
    * Set the registration to REGISTER, else it is handled during the parsing of the configuration file 
    * and we have no guarantee that the properties bean is loaded/exposed yet 
    */ 
    @Override 
    public ConfigurationPhase getConfigurationPhase() { 
     return ConfigurationPhase.REGISTER_BEAN; 
    } 
} 
+0

Ah. Ek Açıklama tabanlı. Bu kesinlikle mükemmel. Ek açıklamaları unuttum ama Spring XML'de aynı şeyi yapmanın bir yolu var mı? –

+0

Hmmm ... http://robertmaldon.blogspot.nl/2007/04/conditionally-defining-spring-beans.html bana bir google verdi (ki hangi sürümde emin değilim) Bu örnekte yay kullanılır, çünkü * oldukça * eski) –

İlgili konular