springBoot注解@Conditional


官方文档定义:“Indicates that a component is only eligible for registration when all specified conditions match”,意思是只有满足一些列条件之后创建一个bean。

@Conditional定义

@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.TYPE, ElementType.METHOD)  
public @interface Conditional{  
lass <!--?extends Condition-->[] value();  
} 
@Conditional注解主要用在以下位置:
  • 类级别可以放在注标识有@Component(包含@Configuration)的类上
  • 作为一个meta-annotation,组成自定义注解
  • 方法级别可以放在标识由@Bean的方法上

如果一个@Configuration的类标记了@Conditional,所有标识了@Bean的方法和@Import注解导入的相关类将遵从这些条件。

 

condition接口定义如下:

public interface Condition{  
/** Determine if the condition matches. 
* @param context the condition context 
* @param metadata meta-data of the {@link AnnotationMetadata class} or 
* {@link Method method} being checked. 
* @return {@code true} if the condition matches and the component can be registered 
* or {@code false} to veto registration. 
*/  
boolean matches(ConditionContext context, AnnotatedTypeMedata metadata);  
}

下面看一个例子:

import org.springframework.context.annotation.Condition;  
import org.springframework.context.annotation.ConditionContext;  
import org.springframework.core.type.AnnotatedTypeMetadata;  
   
public class LinuxCondition implements Condition{  
   
  @Override  
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {  
    return context.getEnvironment().getProperty("os.name").contains("Linux");  }  
} 
import org.springframework.context.annotation.Condition;   
import org.springframework.context.annotation.ConditionContext;   
import org.springframework.core.type.AnnotatedTypeMetadata;   
   
public class WindowsCondition implements Condition{  
   
  @Override   
  public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {  
    return context.getEnvironment().getProperty("os.name").contains("Windows");  
  }  
}  

我们有两个类LinuxCondition 和WindowsCondition 。两个类都实现了Condtin接口,重载的方法返回一个基于操作系统类型的布尔值。

下面我们定义两个bean,一个符合条件另外一个不符合条件:

import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Conditional;  
import org.springframework.context.annotation.Configuration;  
   
@Configuration  
public class MyConfiguration {  
   
  @Bean(name="emailerService")  
  @Conditional(WindowsCondition.class)  
  public EmailService windowsEmailerService(){  
      return new WindowsEmailService();  
  }  
   
  @Bean(name="emailerService")  
  @Conditional(LinuxCondition.class)  
  public EmailService linuxEmailerService(){  
    return new LinuxEmailService();  
  }  
}  

当符合某一个条件的时候,这里的@Bean才会被初始化。 

  


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM