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对象来获取配置 ...
随机推荐
- The C Programming Language Second Edition
%12d at least #include <stdio.h> main() { ,sum=,w=; ; ; w<=end; w++ ) { sum+=w; // for(wb= ...
- Quality of service
w https://en.wikipedia.org/wiki/Quality_of_service Quality of service (QoS) is the overall performan ...
- python的文件处理学习笔记
python的文件处理函数是open() 以下主要是关于这个函数的一些学习笔记 1.文件处理离不开编码 要注意的是文件打开时的编码和文件保存时的编码的统一,这样才能保证你打开的文件不会存在乱码 总结: ...
- 0403-服务注册与发现-客户端负载均衡-Ribbon的基本使用
一.概述 问题1.上一篇文章已说明如何注册微服务,但是调用方如何调用,以及如何防止硬编码.即电影微服务调用用户微服务 问题2.用户微服务多个节点,调用服务方如何负载均衡 二.实现负载均衡方式 2.1. ...
- 剑指offer 面试27题
面试27题: 题目:二叉树的镜像 题:操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / ...
- requirejs源码分析: requirejs 方法–1. 主入口
该方法是 主要的入口点 也是最常用的方法. req = requirejs = function (deps, callback, errback, optional) { //Find the ri ...
- DOM加载过程
静态的dom 动态的dom http://blog.csdn.net/cxiaokai/article/details/7552653 一:预编译 解释 js加 ...
- substring splice
返回start到end之前 不包括end stringObject.substring(start,end) (不接受负数) stringObject.slice(start,end) start起始 ...
- OS X 与传统Unix的一点区别
在传统的Unix系统或者Linux系统中,你是很难在根目录下找到大写开头的文件夹的, 但是看一下OS X: ls / Applications Users etc private var Develo ...
- Oracle表与约束关系
手动回收表的存储方式: SQL> alter table aux_emp deallocate unused; //回收所有未使用的存储空间 表已更改. 回收aux_emp的存储空间,保留50K ...