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 ...
随机推荐
- mysql数据库第三弹
mysql数据库知识拓展 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,并可以将其当作表来使用. SELECT ...
- 【python】__new__和__init__区别
原文:http://blog.csdn.net/cnmilan/article/details/8849680 __new__:创建对象时调用,返回当前对象的一个实例__init__:创建完对象后调用 ...
- 【机器学习】人工神经网络ANN
神经网络是从生物领域自然的鬼斧神工中学习智慧的一种应用.人工神经网络(ANN)的发展经历的了几次高潮低谷,如今,随着数据爆发.硬件计算能力暴增.深度学习算法的优化,我们迎来了又一次的ANN雄起时代,以 ...
- 在64位Win7环境+64位JDK下,运行64位Eclipse,提示“Failed to load the JNI shared library”错误,提示jvm.dll不对
-startup plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar --launcher.library plugins/org.ecl ...
- 实战项目——mysql主从架构的实现
一主一从 1.1 环境准备: centos系统服务器2台. 一台用户做Mysql主服务器, 一台用于做Mysql从服务器, 配置好yum源. 防火墙关闭. 各节点时钟服务同步. 各节点之间可以通过主机 ...
- js 图片转换为base64 (2)
<input type="file" id="testUpload"> <img src="" id="img& ...
- C# 串口接收数据中serialPort.close()死锁
最近在做一个有关高铁模拟仓显示系统的客户端程序,在这个程序中要运用串口serialPort传输数据,因为每次接收数据结束后要更新UI界面,所以就用到了的Invoke,将更新UI的程序代码封装到一个方法 ...
- gulp基础操作实践
按照gulp中文文档对gulp基础操作的一些实践练习,记录以防忘掉. 一,选择并输出文件:gulp.src(globs[,options]) eg:gulp.src('src/less/index.l ...
- Beautifulsoup分解
from urllib.request import Request, ProxyHandler from urllib.request import build_opener from bs4 im ...
- MFC中小笔记
主要记录下一些有啊没啊的MFC东西. 1.单文档 去掉 无标题:在玩的时候用于FindWindow(class,title) BOOL CMainFrame::PreCreateWindow(CREA ...