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 ...
随机推荐
- Kubernetes 学习13 kubernetes pv pvc configmap 和secret
一.概述 1.我们在pvc申请的时候未必就有现成的pv能正好符合这个pvc在申请中指定的条件,毕竟上一次的成功是我们有意设定了有一些满足有一些不满足的前提下我们成功创建了一个pvc并且被pod绑定所使 ...
- loj #6485. LJJ 学二项式定理 单位根反演
新学的黑科技,感觉好nb ~ #include <bits/stdc++.h> #define ll long long #define setIO(s) freopen(s". ...
- ServletContext对象应用——三天免登录
1.用到的知识点: (1)Cookie (2)Session (3)ServletContext 其中Cookie和Session是会话技术的组成部分,一次会话从打开浏览器的某个站点开始,到浏览器关闭 ...
- my.conf
[client] port = 3306 socket = /tmp/mysql.sock [mysqld] port = 3306 socket = /tmp/mysql.sock basedir ...
- Saltstack cmd.run 多项命令
cmd.run 执行多个命令: sls 示例: add_site: cmd.run: - name: | C:\Windows\System32\inetsrv\appcmd.exe delete s ...
- codevs 5972 格子游戏
5972 格子游戏 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description Alice和Bob玩了一个古老的游戏:首先画一个n * ...
- 学习速率过大 or 过小
- 2019暑期金华集训 Day7 分治
自闭集训 Day7 分治 主定理 由于我沉迷调题,这个地方没听课. 某些不等式 咕了 nth_element 使用快速排序的思想,选一个中间点,看左右有多少个. 期望复杂度\(O(n)\). 首先把一 ...
- 数据结构Java版之广度优先图(十三)
广度优先,则是用的队列,将每一层的节点先存入队列中去,后依次取出队列中的节点,显示与当前节点存在边,但是未被访问过的节点,也就是下一层与之相联系的节点,再将这些节点存入队列.经过层层迭代,就可以完全遍 ...
- Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [spring-mybatis.xml]: Initialization of bean failed