17点睛Spring4.1-@Conditional
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的更多相关文章
- 18点睛Spring4.1-Meta Annotation
18.1 Meta Annotation 元注解:顾名思义,就是注解的注解 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码 下面我们用& ...
- 04点睛Spring4.1-资源调用
转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...
- 14点睛Spring4.1-脚本编程
转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...
- 00点睛Spring4.1-环境搭建
转载:https://www.iteye.com/blog/wiselyman-2210250 0.1 前置条件 Spring 4.1提倡基于Java Config和注解的配置,所以本教程通篇不会采用 ...
- 16点睛Spring4.1-TaskScheduler
转发:https://www.iteye.com/blog/wiselyman-2213049 16.1 TaskScheduler 提供对计划任务提供支持; 使用@EnableScheduling开 ...
- 15点睛Spring4.1-TaskExecutor
转发:https://www.iteye.com/blog/wiselyman-2212679 15.1 TaskExecutor spring的TaskExecutor为在spring环境下进行并发 ...
- 13点睛Spring4.1-Spring EL
13.1 Spring EL Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言; 本教程关注于在注解中使用Spring EL; Spring EL ...
- 12点睛Spring4.1-Spring Aware
12.1 Aware 我们设计的准则是解耦,这就意味着我们不能对Spring的IoC容器有直接的依赖,但是我们还是想我们的bean能识别容器的资源; 使用aware能让我们在应用的任意位置获得spri ...
- 11点睛Spring4.1-Property Editor
11.1 Propert Editor property editor是JavaBeans API的一项特性,用来字符和属性值之间的互相转换(如2014-03-02和Date类型的互相转换) spri ...
随机推荐
- Javaweb学习笔记(一)
一.javaweb学习是所需要的细节 1.发送响应头相关的方法 1).addHeader()与setHeader()都是设置HTTP协议的响应头字段,区别是addHeader()方法可以增加同名的响应 ...
- Win10 Subsystem Linux : Ubuntu 的root密码
安装完Ubuntu后忽然意识到没有设置root密码, 不知道密码自然就无法进入根用户下.Ubuntu的默认root密码是随机的, 即每次开机都有一个新的root密码.我们可以在终端输入命令 sudo ...
- WSAStartup() - 使用方法
当一个应用程序调用WSAStartup函数时, 操作系统根据请求的Socket版本来搜索相应的Socket库,然后绑定找到的Socket库到该应用程序中. 以后应用程序就可以调用所请求的Socket库 ...
- RookeyFrame 通用页面 加载数据 原理
说明: 我是一步一步跳转进去的哈 测试的功能:通用列表页面的普通查询 点一下查询按钮,就能看到请求的地址:/DataAsync/LoadGridData.html 1.DataController - ...
- 微信小程序开源
| UI组件 | | | | | | | | | weui-wxss ★1873 - 同微信原生视觉体验一致的基础样式库 | | | | | | zanui-weapp ★794 - 好用易扩展的小程 ...
- (30)打鸡儿教你Vue.js
Vue+Webpack 1.对es6语法有基本了解 2.了解前端工程化 3.了解vuejs 1.通过webpack搭建vue工程workflow 2.哪些是学习vue的重点 3..vue文件开发模式 ...
- Vue.js中 watch的理解以及深度监听
如代码: <div> <p>FullName: {{fullName}}</p> <p>FirstName: <input type=" ...
- ubuntu之路——day9.1 深度学习超参数的调优
参数重要性: 第一阶:α即learning rate 第二阶:momentum中的β,hidden units的数量,mini-batch的大小 第三阶:hidden layers的数量,learni ...
- php-浮点数计算,double类型数加减乘除必须用PHP提供的高精度计算函数
一.前方有坑 php在使用加减乘除等运算符计算浮点数的时候,经常会出现意想不到的结果,特别是关于财务数据方面的计算,给不少工程师惹了很多的麻烦.比如今天工作终于到的一个案例: $a = 2586; $ ...
- Linux tar: Cannot change ownership to [..]: Permission denied
tar xzf $INPUT_FOLDER/archive.tar.gz --no-same-owner -C /mnt/test-nas/