@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的更多相关文章

  1. Spring Boot条件注解

    一.为什么SpringBoot产生于Spring4? Spring4中增加了@Condition annotation, 使用该Annotation之后,在做依赖注入的时候,会检测是否满足某个条件来决 ...

  2. 峰哥说技术: 05-Spring Boot条件注解注解

    Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 05 峰哥说技术  Spring Boot条件注解 @EnableAutoConfiguration开启自 ...

  3. Spring Boot 常用注解汇总

    一.启动注解 @SpringBootApplication @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documen ...

  4. Spring Boot中注解@ConfigurationProperties

    在Spring Boot中注解@ConfigurationProperties有三种使用场景,而通常情况下我们使用的最多的只是其中的一种场景.本篇文章带大家了解一下三种场景的使用情况. 场景一 使用@ ...

  5. Spring Boot常用注解总结

    Spring Boot常用注解总结 @RestController和@RequestMapping注解 @RestController注解,它继承自@Controller注解.4.0之前的版本,Spr ...

  6. 3个Spring Boot核心注解,你知道几个?

    Spring Boot 核心注解讲解 Spring Boot 最大的特点是无需 XML 配置文件,能自动扫描包路径装载并注入对象,并能做到根据 classpath 下的 jar 包自动配置. 所以 S ...

  7. Spring Boot@Component注解下的类无法@Autowired的问题

    title: Spring Boot@Component注解下的类无法@Autowired的问题 date: 2019-06-26 08:30:03 categories: Spring Boot t ...

  8. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

  9. 【SpringBoot】15. Spring Boot核心注解

    Spring Boot核心注解 1 @SpringBootApplication 代表是Spring Boot启动的类 2 @SpringBootConfiguration 通过bean对象来获取配置 ...

随机推荐

  1. JavaScript方法splice()和slice()

    1 splice() 1.1 说明 splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目.该方法会改变原始数组.Link 1.2 语法 arrayObject.splice(inde ...

  2. dev 小问题列表

    1. MemoEdit > Lines Text lines are separated by line feed and carriage return characters ("\ ...

  3. 我的Android进阶之旅------>修改Android签名证书keystore的密码、别名alias以及别名密码

    转载于:http://blog.k-res.net/archives/1229.html  和 http://blog.k-res.net/archives/1671.html ADT允许自定义调试用 ...

  4. 关于like %%的优化思路

    测试数据:2亿行,被筛选出的数据,3KW多行. 众所周知 like %str%无法走索引,但是我们如果实在是有这种需求要达到like '%str%'的筛选目的,怎么优化好一些呢? 以下是我的一些思考: ...

  5. HAProxy配置参数说明

    一.全局配置"global"配置中的参数为进程级别的参数,且通常与其运行的OS相关.1.进程管理及安全相关的参数chroot <jail dir>修改haproxy的工 ...

  6. SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传

    SpringMVC:学习笔记(10)——整合Ckeditor且实现图片上传 配置CKEDITOR 精简文件 解压之后可以看到ckeditor/lang下面有很多语言的js,如果不需要那么多种语言的,可 ...

  7. SpringMVC:学习笔记(5)——数据绑定及表单标签

    SpringMVC——数据绑定及表单标签 理解数据绑定 为什么要使用数据绑定 基于HTTP特性,所有的用户输入的请求参数类型都是String,比如下面表单: 按照我们以往所学,如果要获取请求的所有参数 ...

  8. MySQL 数据库怎样把一个表的数据插入到另一个表

         web开发中,我们经常需要将一个表的数据插入到另外一个表,有时还需要指定导入字段,设置只需要导入目标表中不存在的记录,虽然这些都可以在程序中拆分成简单sql来实现,但是用一个sql的话,会节 ...

  9. CentOS 5下freeswitch中集成使用ekho实现TTS功能三

    四:在freeswitch中调用ekho 注:在测试过程中该语音包好像没用 FreeSWITCH 中文语音包测试版fssounds.zip 在/usr/local/freeswitch/sounds/ ...

  10. linux 基础二---用户群租权限

    用户&群组&权限 一.用户 1.用户及passwd文件 1) 掌握/etc/passwd文件的功能:存储所有用户的相关信息,该文件也被称为用户信息数据库(Database). 2) / ...