17.1 @Conditional

  • @Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean;
  • 应用场景
    • 当某一个jar包在classpath中的时候,配置某几个bean;
    • 当某一个bean配置好后,会自动配置一个特定的bean;
    • 当某种环境变量被设置后,创建某个bean;
  • @Conditional为敏捷开发所提倡的原则"习惯优于配置"提供了支持;
  • @Conditional是Spring Boot快速开发框架实现"习惯优于配置"的核心技术;

17.2 示例

演示在windows和linux系统下,初始化不同的bean,windows和linux作为判断条件;

17.2.1 构造判断条件

条件的构造需要类实现Condition接口,并实现matches方法

  • WindowsCondition
package com.wisely.conditional;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata; public class WindowsCondition implements Condition { public boolean matches(ConditionContext context,
AnnotatedTypeMetadata metadata) {
return context.getEnvironment().getProperty("os.name").contains("Windows");
} }
  • LinuxCondition
package com.wisely.conditional;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata; public class LinuxCondition implements Condition { public boolean matches(ConditionContext context,
AnnotatedTypeMetadata metadata) {
return context.getEnvironment().getProperty("os.name").contains("Linux");
} }

17.2.2 编写不同条件下需要的bean

  • 接口
package com.wisely.conditional;

public interface CommandService {

    public String showListCommand();
}
  • WindowsCommnadService
package com.wisely.conditional;

public class WindowsCommnadService implements CommandService {

    public String showListCommand() {
return "dir";
} }
  • LinuxCommandService
package com.wisely.conditional;

public class LinuxCommandService implements CommandService {

    public String showListCommand() {
return "ls";
} }

17.2.3 编写配置类

package com.wisely.conditional;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; @Configuration
public class DemoConfig {
@Bean
@Conditional(WindowsCondition.class)
public CommandService commandService() {
return new WindowsCommnadService();
} @Bean
@Conditional(LinuxCondition.class)
public CommandService linuxEmailerService() {
return new LinuxCommandService();
} }

17.2.4 测试

  • windows下
package com.wisely.conditional;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.conditional");
CommandService cs = context.getBean(CommandService.class);
System.out.println(cs.showListCommand());
context.close();
}
}

输出结果

dir
  • Linux下(本例没有切换到linux,直接修改os.name为Linux)
package com.wisely.conditional;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
System.setProperty("os.name", "Linux");
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.wisely.conditional");
CommandService cs = context.getBean(CommandService.class);
System.out.println(cs.showListCommand());
context.close(); }
}

输出结果

17点睛Spring4.1-@Conditional的更多相关文章

  1. 18点睛Spring4.1-Meta Annotation

    18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...

  2. 04点睛Spring4.1-资源调用

    转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...

  3. 14点睛Spring4.1-脚本编程

    转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...

  4. 00点睛Spring4.1-环境搭建

    转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...

  5. 16点睛Spring4.1-TaskScheduler

    转发:https://www.iteye.com/blog/wiselyman-2213049 16.1 TaskScheduler 提供对计划任务提供支持; 使用@EnableScheduling开 ...

  6. 15点睛Spring4.1-TaskExecutor

    转发:https://www.iteye.com/blog/wiselyman-2212679 15.1 TaskExecutor spring的TaskExecutor为在spring环境下进行并发 ...

  7. 13点睛Spring4.1-Spring EL

    13.1 Spring EL Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言; 本教程关注于在注解中使用Spring EL; Spring EL ...

  8. 12点睛Spring4.1-Spring Aware

    12.1 Aware 我们设计的准则是解耦,这就意味着我们不能对Spring的IoC容器有直接的依赖,但是我们还是想我们的bean能识别容器的资源; 使用aware能让我们在应用的任意位置获得spri ...

  9. 11点睛Spring4.1-Property Editor

    11.1 Propert Editor property editor是JavaBeans API的一项特性,用来字符和属性值之间的互相转换(如2014-03-02和Date类型的互相转换) spri ...

随机推荐

  1. Linux中三种SCSI target的介绍之SCST

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/scaleqiao/article/deta ...

  2. 洛谷 4290 [HAOI2008]玩具取名 题解

    P4290 [HAOI2008]玩具取名 题目描述 某人有一套玩具,并想法给玩具命名.首先他选择WING四个字母中的任意一个字母作为玩具的基本名字.然后他会根据自己的喜好,将名字中任意一个字母用&qu ...

  3. [SDOI2009][BZOJ 1876]SuperGCD

    Description Sheng bill有着惊人的心算能力,甚至能用大脑计算出两个巨大的数的GCD(最大公约 数)!因此他经常和别人比 赛计算GCD.有一天Sheng bill很嚣张地找到了你,并 ...

  4. select下拉框多选取值

    本来是单选取值,现改为多选 其中<select> 标签新增multiple属性,如<select id = "sel"  multiple = "mul ...

  5. 移动端滚动选择器mobileSelect.js

    一款多功能的移动端滚动选择器,支持单选到多选.支持多级级联.提供自定义回调函数.提供update函数二次渲染.重定位函数.兼容pc端拖拽等等.. 特性 原生js移动端选择控件,不依赖任何库 可传入普通 ...

  6. python技巧获取26个英语字母

    import string string.ascii_uppercase # 获取26个大写字母 string.ascii_lowercase # 获取26个小写字母 string.ascii_let ...

  7. 开源JS图片裁剪插件

    开源JS图片裁剪插件 一.总结 一句话总结: 要用点赞最高的插件,这样适用性最好,效果最好,出问题的概率也最低,这里电脑端和手机端都可以用的建议用 cropper.js 二.5款好用的开源JS图片裁剪 ...

  8. Traverse an expression tree and extract parameters

    Traverse an expression tree and extract parameters   I think as you've said that using ExpressionVis ...

  9. Java中JVM内存结构

    Java中JVM内存结构 线程共享区 方法区: 又名静态成员区域,包含整个程序的 class.static 成员等,类本身的字节码是静态的:它会被所有的线程共享和是全区级别的: 属于共享内存区域,存储 ...

  10. web文件上传,带进度条

    原生ajax上传带进度条 (百分比) <%@ page language="java" contentType="text/html; charset=UTF-8& ...