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. postgreSQL \timing 查看SQL执行时间

    postgreSQL中,关于查询执行时间的\timing的使用

  2. Bzoj 2875: [Noi2012]随机数生成器(矩阵乘法)

    2875: [Noi2012]随机数生成器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2052 Solved: 1118 Description ...

  3. 【一起来烧脑】一步React.JS学会体系

    [外链图片转存失败(img-cn4fbVDq-1563575047348)(https://upload-images.jianshu.io/upload_images/11158618-8c6f3d ...

  4. Chrome教程(一)NetWork面板分析网络请求

    官方文档:https://developers.google.com/web/tools/chrome-devtools/network/ 1.如何打开 无论是在Windows还是Mac,都可以使用( ...

  5. Navicat premium查看数据库表中文注释的两种方式

    有时候我需要查看数据库表中文注释,来确定每个表存的是哪个模块的数据,确保测试时对数据库查询操作无误. 这个操作我忘记了,此处做一个记录 方式一:通过sql语句来,前提是你知道是哪个表,这种方式不容易改 ...

  6. [洛谷 P4556] 雨天的尾巴

    传送门 Solution 线段树合并的入门题 lca可以在dfs的时候离线求(用并查集) 更新的点有每条链的两个端点,它们的lca和dad[lca] 为了节省空间,lca和dad[lca]的更新可以先 ...

  7. [代码审计]PHP_Bugs题目总结(2)

    写的有点多了,上一篇放在一起显得有点臃肿,就再起一篇吧~ 迷路的老铁点这里:[代码审计]PHP_Bugs题目总结(1) 0x14 intval函数四舍五入 <?php if($_GET[id]) ...

  8. springboot项目获取resource下的文件

    package com.expr.exceldemo; import org.springframework.core.io.ClassPathResource; public class Test ...

  9. go中的方法以及自定义类型代码示例

    package main import "fmt" type user struct { name string age int sex string } type admin s ...

  10. spark_API

    1.概述 总的来讲,每一个spark驱动程序应用都由一个驱动程序组成,该驱动程序包含一个由用户编写的main方法,该方法会在集群上执行一些并行计算操作.Spark最重要的一个概念是弹性分布式数据集,简 ...