1. package com.atguigu.config;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Conditional;
  5. import org.springframework.context.annotation.Configuration;
  6.  
  7. import com.atguigu.bean.Person;
  8. import com.atguigu.condition.LinuxCondition;
  9. import com.atguigu.condition.WindowsCondition;
  10. import com.atguigu.test.IOCTest;
  11.  
  12. //类中组件统一设置。满足当前条件,这个类中配置的所有bean注册都能生效;
  13. @Conditional({WindowsCondition.class})
  14. @Configuration
  15. public class MainConfig2 {
  16. /**
  17. * @Conditional({Condition}) : 按照一定的条件进行判断,满足条件给容器中注册bean
  18. *
  19. * 如果系统是windows,给容器中注册("bill")
  20. * 如果是linux系统,给容器中注册("linus")
  21. */
  22. @Conditional({WindowsCondition.class})
  23. @Bean("bill")
  24. public Person person01(){
  25. return new Person("Bill Gates",62);
  26. }
  27.  
  28. @Conditional(LinuxCondition.class)
  29. @Bean("linus")
  30. public Person person02(){
  31. return new Person("linus", 48);
  32. }
  33. }

2.条件1

  1. package com.atguigu.condition;
  2.  
  3. import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
  4. import org.springframework.beans.factory.support.BeanDefinitionRegistry;
  5. import org.springframework.context.annotation.Condition;
  6. import org.springframework.context.annotation.ConditionContext;
  7. import org.springframework.core.env.Environment;
  8. import org.springframework.core.type.AnnotatedTypeMetadata;
  9.  
  10. //判断是否linux系统
  11. public class LinuxCondition implements Condition {
  12.  
  13. /**
  14. * ConditionContext:判断条件能使用的上下文(环境)
  15. * AnnotatedTypeMetadata:注释信息
  16. */
  17. @Override
  18. public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
  19. // TODO是否linux系统
  20. //1、能获取到ioc使用的beanfactory
  21. ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
  22. //2、获取类加载器
  23. ClassLoader classLoader = context.getClassLoader();
  24. //3、获取当前环境信息
  25. Environment environment = context.getEnvironment();
  26. //4、获取到bean定义的注册类
  27. BeanDefinitionRegistry registry = context.getRegistry();
  28.  
  29. String property = environment.getProperty("os.name");
  30.  
  31. //可以判断容器中的bean注册情况,也可以给容器中注册bean
  32. boolean definition = registry.containsBeanDefinition("person");
  33. if(property.contains("linux")){
  34. return true;
  35. }
  36.  
  37. return false;
  38. }
  39.  
  40. }

3.条件2

  1. package com.atguigu.condition;
  2.  
  3. import org.springframework.context.annotation.Condition;
  4. import org.springframework.context.annotation.ConditionContext;
  5. import org.springframework.core.env.Environment;
  6. import org.springframework.core.type.AnnotatedTypeMetadata;
  7.  
  8. //判断是否windows系统
  9. public class WindowsCondition implements Condition {
  10.  
  11. @Override
  12. public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
  13. Environment environment = context.getEnvironment();
  14. String property = environment.getProperty("os.name");
  15. if(property.contains("Windows")){
  16. return true;
  17. }
  18. return false;
  19. }
  20.  
  21. }

spring注解第03课 按条件加载Bean @Conditional的更多相关文章

  1. 【Spring注解驱动开发】使用@PropertySource加载配置文件,我只看这一篇!!

    写在前面 很多小伙伴都在问:冰河,你的Spring专题更新完了吗?怎么感觉像是写了一半啊?我:没有更新完呀,整个专题预计会有70多篇.那怎么更新了一半就去写别的了呢?那是因为有很多其他的小伙伴在后台留 ...

  2. Spring多种加载Bean方式简析

    1 定义bean的方式 常见的定义Bean的方式有: 通过xml的方式,例如: <bean id="dictionaryRelMap" class="java.ut ...

  3. 【Spring源码分析】非懒加载的单例Bean初始化过程(下篇)

    doCreateBean方法 上文[Spring源码分析]非懒加载的单例Bean初始化过程(上篇),分析了单例的Bean初始化流程,并跟踪代码进入了主流程,看到了Bean是如何被实例化出来的.先贴一下 ...

  4. Spring源码分析:非懒加载的单例Bean初始化过程(下)

    上文Spring源码分析:非懒加载的单例Bean初始化过程(上),分析了单例的Bean初始化流程,并跟踪代码进入了主流程,看到了Bean是如何被实例化出来的.先贴一下AbstractAutowireC ...

  5. spring加载bean流程解析

    spring作为目前我们开发的基础框架,每天的开发工作基本和他形影不离,作为管理bean的最经典.优秀的框架,它的复杂程度往往令人望而却步.不过作为朝夕相处的框架,我们必须得明白一个问题就是sprin ...

  6. Bean 注解(Annotation)配置(1)- 通过注解加载Bean

    Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...

  7. 【Spring源码分析】非懒加载的单例Bean初始化过程(上篇)

    代码入口 上文[Spring源码分析]Bean加载流程概览,比较详细地分析了Spring上下文加载的代码入口,并且在AbstractApplicationContext的refresh方法中,点出了f ...

  8. 【Spring源码分析】非懒加载的单例Bean初始化前后的一些操作

    前言 之前两篇文章[Spring源码分析]非懒加载的单例Bean初始化过程(上篇)和[Spring源码分析]非懒加载的单例Bean初始化过程(下篇)比较详细地分析了非懒加载的单例Bean的初始化过程, ...

  9. 【死磕 Spring】----- IOC 之 加载 Bean

    原文出自:http://cmsblogs.com 先看一段熟悉的代码: ClassPathResource resource = new ClassPathResource("bean.xm ...

随机推荐

  1. linux开发板出现Read-only file system的解决办法

    @2018-11-29 创建文件夹出现如下提示 mkdir: can't create directory 'test': Read-only file system 使用命令 mount rw -o ...

  2. 按奇偶排序数组 II

    题目描述 给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数. 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数:当 A[i] 为偶数时, i 也是偶数. 你可以返回任何满足上述 ...

  3. 这些保护Spring Boot 应用的方法,你都用了吗?

    这些保护Spring Boot 应用的方法,你都用了吗? 生如夏花 SpringForAll社区 今天 Spring Boot大大简化了Spring应用程序的开发.它的自动配置和启动依赖大大减少了开始 ...

  4. linux free命令

    Linux上的free命令详解 free命令的所有输出值都是从/proc/meminfo中读出的 total used free shared buffers cached Mem: -/+ buff ...

  5. Win32 SDK:ListBox 为什么不整个 LB_SETTEXT

    Win32 & SDK: ListBox 有个 LB_GETTEXT,为什么没有 LB_SETTEXT,想修改指定Item的Text,还真不容易. 自已写了一个,支持单选和多选模式,部分代码: ...

  6. django框架中的全文检索Haystack

    1.什么是Haystack Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高 ),该框架支持Solr,Elasticsearch,Whoosh ...

  7. 把本人基于Dubbo的毕业设计分享粗来~

    这个毕业设计不是太让我满意,可能时间关系吧,但是现在工作了我也懒得没时间去完善了[目前也就有一个功能没完成,就是给产品经理送绿帽子的模块] 项目地址:GitHub 技术栈:Spring+SpringM ...

  8. toString()和toLocaleString()有什么区别

    偶然之间用到这两个方法 然后在数字转换成字符串的时候,并没有感觉这两个方法有什么区别,如下: 1 2 3 4 5 6 7 8 var e=123     e.toString() "123& ...

  9. Qt ------ 添加某个功能,比如(QSerialPort)注意事项

    添加某个功能,比如(QSerialPort),不仅需要保护头文件 还需要在.pro文件内添加 QT += serialport

  10. icmpsh解决运行报错

    运行./run.sh报错 sysctl -w net.ipv4.icmp_echo_ignore_all=1 >/dev/null python icmpsh_m.py 攻击机ip 目标机ip ...