博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springBoot注解@Conditional
阅读量:7044 次
发布时间:2019-06-28

本文共 2507 字,大约阅读时间需要 8 分钟。

hot3.png

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

@Conditional定义

@Retention(RetentionPolicy.RUNTIME)  @Target(ElementType.TYPE, ElementType.METHOD)  public @interface Conditional{  lass 
[] 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才会被初始化。 

转载于:https://my.oschina.net/xiaominmin/blog/1607488

你可能感兴趣的文章
Spring Cloud构建微服务架构—服务网关过滤器
查看>>
Git命令
查看>>
插入一条不重复的记录
查看>>
挖矿程序minerd,wnTKYg***分析和解决
查看>>
CSS:haslayout解说
查看>>
数组加1 Plus One
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
关于基于Annotation的Advice
查看>>
others powercli scripts1
查看>>
python 类 五 : 多重继承的MRO顺序
查看>>
Asp.net 数据库链接字符串 备份一下
查看>>
CentOS系统安装详细步骤
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
Shell脚本之测试及条件表达式简述
查看>>
JavaScript函数详解(二)
查看>>
X9BYOD集群界面展示
查看>>
WebsitePanel部署指南
查看>>
Python 字符串格式化 (%操作符)
查看>>