spring boot: 条件注解@Condition
@Conditional根据满足某一个特定的条件创建一个特定的Bean(基于条件的Bean的创建,即使用@Conditional注解)。
比方说,当一个jar包在一个类的路径下的时候,自动配置一个或多个Bean,或者只有某个Bean被创建才会去创建另外一个Bean。
通过实现Condition接口,并重写期matches方法来构造判断条件。若在windows系统洗运行程序,则输出列表命令dir,若在linux操作系统下运行程序,则输出命令为:ls.
1.判断条件定义
判断windows的条件
package ch2.conditional2; //条件注解,并复写此类
import org.springframework.context.annotation.Condition;
//条件注解容器
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata; public class LinuxCondition implements Condition { @Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// TODO Auto-generated method stub
return context.getEnvironment().getProperty("os.name").contains("Linux");
} }
判定linux的条件
package ch2.conditional2; //条件注解接口类,复写metches
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata; public class WindowsCondition implements Condition { @Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// TODO Auto-generated method stub
return context.getEnvironment().getProperty("os.name").contains("Windows");
} }
2不同系统下的Bean类
1接口
package ch2.conditional2;
public interface ListService {
public String showListCmd();
}
2windows下要创建的Bean类
package ch2.conditional2;
import ch2.conditional2.ListService;
public class WindowsListService implements ListService {
@Override
public String showListCmd() {
// TODO Auto-generated method stub
return "dir";
}
}
3linux下要创建的Bean类
package ch2.conditional2;
import ch2.conditional2.ListService;
public class LinuxListService implements ListService {
@Override
public String showListCmd() {
// TODO Auto-generated method stub
return "ls";
}
}
3配置类
package ch2.conditional2; //配置类声明
import org.springframework.context.annotation.Configuration;
//bean声明
import org.springframework.context.annotation.Bean;
//条件限制
import org.springframework.context.annotation.Conditional; //声明为配置类
@Configuration
public class ConditionConfig { //注解为Bean
@Bean
//注解条件判断
@Conditional(WindowsCondition.class)
public ListService windowsListService()
{
return new WindowsListService();
} //注解为Bean
@Bean
//注解条件判断
@Conditional(LinuxCondition.class)
public ListService linuxListService()
{
return new LinuxListService();
}
}
4运行
package ch2.conditional2;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args)
{
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConditionConfig.class);
ListService listService = context.getBean(ListService.class);
System.out.println(
context.getEnvironment().getProperty("os.name") +
"系统的查看目录的命令是: " +
listService.showListCmd()
);
context.close();
}
}
运行结果:Windows 10系统的查看目录的命令是: dir
spring boot: 条件注解@Condition的更多相关文章
- Spring Boot条件注解
一.为什么SpringBoot产生于Spring4? Spring4中增加了@Condition annotation, 使用该Annotation之后,在做依赖注入的时候,会检测是否满足某个条件来决 ...
- 峰哥说技术: 05-Spring Boot条件注解注解
Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 05 峰哥说技术 Spring Boot条件注解 @EnableAutoConfiguration开启自 ...
- Spring Boot 常用注解汇总
一.启动注解 @SpringBootApplication @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documen ...
- Spring Boot中注解@ConfigurationProperties
在Spring Boot中注解@ConfigurationProperties有三种使用场景,而通常情况下我们使用的最多的只是其中的一种场景.本篇文章带大家了解一下三种场景的使用情况. 场景一 使用@ ...
- Spring Boot常用注解总结
Spring Boot常用注解总结 @RestController和@RequestMapping注解 @RestController注解,它继承自@Controller注解.4.0之前的版本,Spr ...
- 3个Spring Boot核心注解,你知道几个?
Spring Boot 核心注解讲解 Spring Boot 最大的特点是无需 XML 配置文件,能自动扫描包路径装载并注入对象,并能做到根据 classpath 下的 jar 包自动配置. 所以 S ...
- Spring Boot@Component注解下的类无法@Autowired的问题
title: Spring Boot@Component注解下的类无法@Autowired的问题 date: 2019-06-26 08:30:03 categories: Spring Boot t ...
- Spring boot 基于注解方式配置datasource
Spring boot 基于注解方式配置datasource 编辑 Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...
- 【SpringBoot】15. Spring Boot核心注解
Spring Boot核心注解 1 @SpringBootApplication 代表是Spring Boot启动的类 2 @SpringBootConfiguration 通过bean对象来获取配置 ...
随机推荐
- /cloudmonitor.log 主机监控
989866842 INFO 2018-09-30 01:38:58.58 [ricGatherServiceHttp] 提交指标完成,耗时:18ms. SystemInfo [serialNumbe ...
- _utf8_encode _utf8_decode base64_encode base64_decode
const Base64 = { // private property _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv ...
- PAT 1070. 结绳(25)
给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连.每次串连后,原来两段绳子的长度 ...
- boost之定时器和io_service
1.定时器的使用,sleep是等待线程,asio封装了操作系统的异步系统调用select,epoll. io_servie 实现了一个任务队列,这里的任务就是void(void)的函数.Io_serv ...
- 关于 Content-Type:application/x-www-form-urlencoded 和 Content-Type:multipart/related(转)
转至:http://www.cnblogs.com/taoys/archive/2010/12/30/1922186.html application/x-www-form-urlencoded: 窗 ...
- php 数组 高效随机抽取指定条记录的算法
php使用数组array_rand()函数进行高效随机抽取指定条数的记录,可以随机抽取数据库中的记录,适合进行随机展示和抽奖程序. 该算法主要是利用php的array_rand()函数,下面看一下ar ...
- MySQL忘记密码的正确解决方法
MySQL忘记密码解决方案:破解本地密码: Windows: 1.用系统管理员登陆系统. 2.停止MySQL的服务. 3.进入命令窗口,然后进入 MySQL的安装目录,比如我的安装目录是c:\mysq ...
- Python基础(12)_python模块之sys模块、logging模块、序列化json模块、pickle模块、shelve模块
5.sys模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的版本信息 ...
- 使用新浪IP库获取IP详细地址
使用新浪IP库获取IP详细地址 <?php class Tool{ /** * 获取IP的归属地( 新浪IP库 ) * * @param $ip String IP地址:112.65.102.1 ...
- 17 南宁区域赛 F - The Chosen One 【规律】
题目链接 https://nanti.jisuanke.com/t/19972 题意 给出一个n 然后将 n 个数 标号为 1 -> n 按顺序排列 每次抽掉 奇数位的数 然后求最后剩下那个数字 ...