@Conditional根据满足某一个特定的条件创建一个特定的Bean(基于条件的Bean的创建,即使用@Conditional注解)。

比方说,当一个jar包在一个类的路径下的时候,自动配置一个或多个Bean,或者只有某个Bean被创建才会去创建另外一个Bean。

通过实现Condition接口,并重写期matches方法来构造判断条件。若在windows系统洗运行程序,则输出列表命令dir,若在linux操作系统下运行程序,则输出命令为:ls.

1.判断条件定义

判断windows的条件

 package ch2.conditional2;

 //条件注解,并复写此类
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) {
// TODO Auto-generated method stub
return context.getEnvironment().getProperty("os.name").contains("Linux");
} }

判定linux的条件

 package ch2.conditional2;

 //条件注解接口类,复写metches
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) {
// TODO Auto-generated method stub
return context.getEnvironment().getProperty("os.name").contains("Windows");
} }

2不同系统下的Bean类

1接口

 package ch2.conditional2;

 public interface ListService {

     public String showListCmd();
}

2windows下要创建的Bean类

 package ch2.conditional2;

 import ch2.conditional2.ListService;

 public class WindowsListService implements ListService {

     @Override
public String showListCmd() {
// TODO Auto-generated method stub
return "dir";
} }

3linux下要创建的Bean类

 package ch2.conditional2;

 import ch2.conditional2.ListService;

 public class LinuxListService implements ListService {

     @Override
public String showListCmd() {
// TODO Auto-generated method stub
return "ls";
} }

3配置类

package ch2.conditional2;

//配置类声明
import org.springframework.context.annotation.Configuration;
//bean声明
import org.springframework.context.annotation.Bean;
//条件限制
import org.springframework.context.annotation.Conditional; //声明为配置类
@Configuration
public class ConditionConfig { //注解为Bean
@Bean
//注解条件判断
@Conditional(WindowsCondition.class)
public ListService windowsListService()
{
return new WindowsListService();
} //注解为Bean
@Bean
//注解条件判断
@Conditional(LinuxCondition.class)
public ListService linuxListService()
{
return new LinuxListService();
}
}

4运行

package ch2.conditional2;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args)
{ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConditionConfig.class);
ListService listService = context.getBean(ListService.class);
System.out.println(
context.getEnvironment().getProperty("os.name") +
"系统的查看目录的命令是: " +
listService.showListCmd()
);
context.close();
}
}

运行结果:Windows 10系统的查看目录的命令是: dir

spring boot: 条件注解@Condition的更多相关文章

  1. Spring Boot条件注解

    一.为什么SpringBoot产生于Spring4? Spring4中增加了@Condition annotation, 使用该Annotation之后,在做依赖注入的时候,会检测是否满足某个条件来决 ...

  2. 峰哥说技术: 05-Spring Boot条件注解注解

    Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 05 峰哥说技术  Spring Boot条件注解 @EnableAutoConfiguration开启自 ...

  3. Spring Boot 常用注解汇总

    一.启动注解 @SpringBootApplication @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documen ...

  4. Spring Boot中注解@ConfigurationProperties

    在Spring Boot中注解@ConfigurationProperties有三种使用场景,而通常情况下我们使用的最多的只是其中的一种场景.本篇文章带大家了解一下三种场景的使用情况. 场景一 使用@ ...

  5. Spring Boot常用注解总结

    Spring Boot常用注解总结 @RestController和@RequestMapping注解 @RestController注解,它继承自@Controller注解.4.0之前的版本,Spr ...

  6. 3个Spring Boot核心注解,你知道几个?

    Spring Boot 核心注解讲解 Spring Boot 最大的特点是无需 XML 配置文件,能自动扫描包路径装载并注入对象,并能做到根据 classpath 下的 jar 包自动配置. 所以 S ...

  7. Spring Boot@Component注解下的类无法@Autowired的问题

    title: Spring Boot@Component注解下的类无法@Autowired的问题 date: 2019-06-26 08:30:03 categories: Spring Boot t ...

  8. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

  9. 【SpringBoot】15. Spring Boot核心注解

    Spring Boot核心注解 1 @SpringBootApplication 代表是Spring Boot启动的类 2 @SpringBootConfiguration 通过bean对象来获取配置 ...

随机推荐

  1. float 的先后顺序 理解流

    <!DOCTYPE html><html><head><style> p{float:right;}</style></head> ...

  2. 【题解】CF611H New Year and Forgotten Tree

    [题解]CF611H New Year and Forgotten Tree 神题了... 题目描述 给定你一棵树,可是每个节点上的编号看不清了,只能辨别它的长度.现在用问号的个数代表每个节点编号那个 ...

  3. 【25】session练习

    #用登录练习session html1为首页,html2为登录页面 def session1(request): uname=request.session.get('myname','请登录') # ...

  4. 如何在有input() 语句下断点调试(内附高清无码福利)

    困扰了半天,一直没找到如何在含有输入语句的情况下用pycharm进行断点调试(调试的同时进行输入交互), But 经过尝试,还是找到了~~~ 通过debug可以快速的找到报错信息,以及观察程序每步的运 ...

  5. 设计一个算法,採用BFS方式输出图G中从顶点u到v的最短路径(不带权的无向连通图G採用邻接表存储)

    思想:图G是不带权的无向连通图.一条边的长度计为1,因此,求带顶点u和顶点v的最短的路径即求顶点u和顶点v的边数最少的顶点序列.利用广度优先遍历算法,从u出发进行广度遍历,类似于从顶点u出发一层一层地 ...

  6. attention机制七搞八搞

    注意力机制即Attention mechanism在序列学习任务上具有巨大的提升作用,在编解码器框架内,通过在编码段加入A模型,对源数据序列进行数据加权变换,或者在解码端引入A模型,对目标数据进行加权 ...

  7. 判断IP地址是否合法

    /* return 1 if string contain only digits, else return 0 */ int valid_digit(char *ip_str) { while (* ...

  8. when you are old

    When you are old william Butler Yeats When you are old and grey and full of sleep And nodding by the ...

  9. qemu-img 的使用

    qemu-img是QEMU的磁盘管理工具,在qemu-kvm源码编译后就会默认编译好qemu-img这个二进制文件.qemu-img也是QEMU/KVM使用过程中一个比较重要的工具,本节对其用法和实践 ...

  10. 【AWS】AWS云计算赋能数字化转型专题研讨会

    点我查看详情 欢迎莅临