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 ...
随机推荐
- SQL---索引---创建索引
CREATE INDEX 语句用于在表中创建索引. 在不读取整个表的情况下,索引使数据库应用程序可以更快地查找数据. 索引 您可以在表中创建索引,以便更加快速高效地查询数据. 用户无法看到索引,它们只 ...
- openface 训练数据集
训练深度网络模型OpenFace还不是运用faceNet的model作为训练模型,所以在准确性上比faceNet要低,如果你只是做一个简单的分类,建议你看看官网的demo3(http://cmusat ...
- webpack之前端性能优化(史上最全,不断更新中。。。)
最近在用webpack优化首屏加载性能,通过几种插件之后我们上线前后的速度快了一倍,在此就简单的分享下吧,先上个优化前后首屏渲染的对比图. 可以看到总下载时间从3800ms缩短到1600ms. 我们在 ...
- android动画基础之Animation
android 动画 摘要: 概述 最近总结一下Android的一些东西,毕竟基础不牢地动山摇.本篇主要涉及Animation,对Tween和Frame动画做些总结. Tween Tween动画即补间 ...
- 面向对象编程 —— java实现函数求导
文章目录 ★引子 ★求导 ★最初的想法 ★初步的想法 ★后来的想法 ★最后的想法 ★编程范式 ★结尾 首先声明一点,本文主要介绍的是面向对象(OO)的思想,顺便谈下函数式编程,而不是教你如何准确地.科 ...
- This package contains sshd, pcal, mysql-client on Ubuntu14:04
[How to build:]cd /home/ops/work/demo/docker/aws/ubuntutouch Dockerfiledocker build -t ubuntu_base:v ...
- qt学习教程1.qt开发环境搭建
qt学习教程1.qt开发环境搭建 首先下载qt 下载地址:http://download.qt.io/archive/qt/ 此教程使用的版本为5.1.1 下载好后,打开安装包,然后点下一步 选择一个 ...
- jdk1.8hashmap源码解析
/* * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETA ...
- [C#]使用Process的StandardInput与StandardOutput写入读取控制台数据
本文为原创文章.源代码为原创代码,如转载/复制,请在网页/代码处明显位置标明原文名称.作者及网址,谢谢! 开发工具:VS2017 语言:C# DotNet版本:.Net FrameWork 4.0及以 ...
- 51nod 1058 N的阶乘的长度 位数公式
1058 N的阶乘的长度基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注输入N求N的阶乘的10进制表示的长度.例如6! = 720,长度为3.Input输入N( ...