spring-boot学习笔记之Conditional
今天看了@Conditional,自己根据以下文章练了下,根据自己的理解操作的



转载出处:http://wiselyman.iteye.com/blog/2213054
17.1 @Conditional
@Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean;
应用场景
当某一个jar包在classpath中的时候,配置某几个bean;
当某一个bean配置好后,会自动配置一个特定的bean;
当某种环境变量被设置后,创建某个bean;
@Conditional为敏捷开发所提倡的原则"习惯优于配置"提供了支持;
@Conditional是Spring Boot快速开发框架实现"习惯优于配置"的核心技术;
演示在windows和linux系统下,初始化不同的bean,windows和linux作为判断条件;
条件的构造需要类实现Condition接口,并实现matches方法
WindowsCondition
packagecom.wisely.conditional;importorg.springframework.context.annotation.Condition;importorg.springframework.context.annotation.ConditionContext;importorg.springframework.core.type.AnnotatedTypeMetadata;publicclassWindowsConditionimplementsCondition{publicbooleanmatches(ConditionContextcontext,AnnotatedTypeMetadatametadata) {returncontext.getEnvironment().getProperty("os.name").contains("Windows"); }}
LinuxCondition
packagecom.wisely.conditional;importorg.springframework.context.annotation.Condition;importorg.springframework.context.annotation.ConditionContext;importorg.springframework.core.type.AnnotatedTypeMetadata;publicclassLinuxConditionimplementsCondition{publicbooleanmatches(ConditionContextcontext,AnnotatedTypeMetadatametadata) {returncontext.getEnvironment().getProperty("os.name").contains("Linux"); }}
接口
packagecom.wisely.conditional;publicinterfaceCommandService{publicStringshowListCommand();}
WindowsCommnadService
packagecom.wisely.conditional;publicclassWindowsCommnadServiceimplementsCommandService{publicStringshowListCommand() {return"dir"; }}
LinuxCommandService
packagecom.wisely.conditional;publicclassLinuxCommandServiceimplementsCommandService{publicStringshowListCommand() {return"ls"; }}
packagecom.wisely.conditional;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Conditional;importorg.springframework.context.annotation.Configuration;@ConfigurationpublicclassDemoConfig{@Bean@Conditional(WindowsCondition.class)publicCommandServicecommandService() {returnnewWindowsCommnadService(); }@Bean@Conditional(LinuxCondition.class)publicCommandServicelinuxEmailerService() {returnnewLinuxCommandService(); }}
windows下
packagecom.wisely.conditional;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;publicclassMain{publicstaticvoidmain(String[]args) {AnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext("com.wisely.conditional");CommandServicecs=context.getBean(CommandService.class);System.out.println(cs.showListCommand()); context.close(); }}
输出结果
dir
Linux下(本例没有切换到linux,直接修改os.name为Linux)
packagecom.wisely.conditional;importorg.springframework.context.annotation.AnnotationConfigApplicationContext;publicclassMain{publicstaticvoidmain(String[]args) {System.setProperty("os.name","Linux");AnnotationConfigApplicationContextcontext=newAnnotationConfigApplicationContext("com.wisely.conditional");CommandServicecs=context.getBean(CommandService.class);System.out.println(cs.showListCommand()); context.close(); }}
输出结果
ls
原文链接:
http://www.jianshu.com/p/4920f6a47a14
spring-boot学习笔记之Conditional的更多相关文章
- Spring Boot学习笔记2——基本使用之最佳实践[z]
前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...
- Spring Boot 学习笔记(六) 整合 RESTful 参数传递
Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...
- Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用[z]
前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文 ...
- Spring Boot 学习笔记1---初体验之3分钟启动你的Web应用
前言 早在去年就简单的使用了一下Spring Boot,当时就被其便捷的功能所震惊.但是那是也没有深入的研究,随着其在业界被应用的越来越广泛,因此决定好好地深入学习一下,将自己的学习心得在此记录,本文 ...
- Spring Boot学习笔记二
Spring Boot入门第二篇 第一天的详见:https://www.cnblogs.com/LBJLAKERS/p/12001253.html 同样是新建一个pring Initializer快速 ...
- Spring Boot 学习笔记--整合Thymeleaf
1.新建Spring Boot项目 添加spring-boot-starter-thymeleaf依赖 <dependency> <groupId>org.springfram ...
- 我的第一个spring boot程序(spring boot 学习笔记之二)
第一个spring boot程序 写在前面:鉴于spring注解以及springMVC的配置有大量细节和知识点,在学习理解之后,我们将直接进入spring boot的学习,在后续学习中用到注解及其他相 ...
- Java框架spring Boot学习笔记(六):Spring Boot事务管理
SpringBoot和Java框架spring 学习笔记(十九):事务管理(注解管理)所讲的类似,使用@Transactional注解便可以轻松实现事务管理.
- Spring Boot学习笔记---Spring Boot 基础及使用idea搭建项目
最近一段时间一直在学习Spring Boot,刚进的一家公司也正好有用到这个技术.虽然一直在学习,但是还没有好好的总结,今天周末先简单总结一下基础知识,等有时间再慢慢学习总结吧. Spring Boo ...
- spring boot学习笔记2
开场知识: spring 容器注入bean,时容器初始化的一些接口以及接口调用的时间先后顺序: 1)BeanFactoryPostProcessor 容器初始化的回调方法 * BeanFactoryP ...
随机推荐
- linux共享文件
首先我们先创建一个组名为workgroup sudo groupadd workgroup 下面给我们这个团队创建两个用户 sudo useradd -G workgroup lucy sudo pa ...
- LKD: Chapter 8 Bottom Halves and Deferring Work
In 2.6.x, there are 3 mechanisms for implementing a bottom half: softirqs, tasklets and work queues. ...
- 问题(一)---线程池,锁、堆栈和Hashmap相关
一.线程池: 多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力. 假设一个服务器完成一项任务所需时间为:T1 创建线程时间,T2 在线程中 ...
- 从MVC到Ajax再到前后端分离的思考
前言 一位小妹去面试前端,前端leader问了"什么是ajax?",答:"接收后台的数据,然后然后自己填充和渲染样式":一位小哥去面试后台,技术经理问了&quo ...
- HDU2159--二维费用背包,三重背包
FATE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- yii2.0框架debug模式
- c++简单编写线性表(实验)
哇 好久没更博客了 因为从上次更文到现在为止,我一直在学c#,然后也觉得没什么太大的东西想写在博客上, 总之这次就简单的传一下学校安排的数据结构的实验,我还是学生,是新手,希望能继续跟大家一起学习, ...
- 前端开发者常用的9个JavaScript图表库
当前,数据可视化已经成为数据科学领域非常重要的一部分.不同网络系统中产生的数据,都需要经过适当的可视化处理,以便更好的呈现给用户读取和分析. 对任何一个组织来说,如果能够充分的获取数据.可视化数据和分 ...
- JAVA基础再回首(二十五)——Lock锁的使用、死锁问题、多线程生产者和消费者、线程池、匿名内部类使用多线程、定时器、面试题
JAVA基础再回首(二十五)--Lock锁的使用.死锁问题.多线程生产者和消费者.线程池.匿名内部类使用多线程.定时器.面试题 版权声明:转载必须注明本文转自程序猿杜鹏程的博客:http://blog ...
- Android研究之监听自身应用被卸载代码实现
1.通过jni实现函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 3 ...