四、Spring中使用@Conditional按照条件注册Bean
以前其实是写过@Condtional注解的笔记的,这里附上链接:
Spring中的@conditional注解
但已经忘记的差不多了,所以今天再重新学习下,可以互补着学习
@Contional:按照一定的条件进行判断,满足条件给容器中注册bean
首先准备一个person实体类,具体代码不列了,前几篇博文中都有。
要使用@Condtional注解,必须实现Conditon接口,并实现其抽象方法mathes,也即用来判断条件的。
然后是准备配置类:
@Configuration
public class MainConfig2 {
//如果系统是windows,给容器中注册("bill")
//如果是linux系统,给容器中注册("linus")
@Conditional({WindowsCondition.class})
@Bean("bill")
public Person person01(){
return new Person("Bill Gates",62);
}
@Conditional(LinuxCondition.class)
@Bean("linus")
public Person person02(){
return new Person("linus", 48);
}
}
//判断是否windows系统
public class WindowsCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Environment environment = context.getEnvironment();
String property = environment.getProperty("os.name");
if(property.contains("Windows")){
return true;
}
return false;
}
}
//判断是否linux系统
public class LinuxCondition implements Condition {
/**
* ConditionContext:判断条件能使用的上下文(环境)
* AnnotatedTypeMetadata:注释信息
*/
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
// TODO是否linux系统
//1、能获取到ioc使用的beanfactory
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
//2、获取类加载器
ClassLoader classLoader = context.getClassLoader();
//3、获取当前环境信息
Environment environment = context.getEnvironment();
//4、获取到bean定义的注册类
BeanDefinitionRegistry registry = context.getRegistry();
String property = environment.getProperty("os.name");
//可以判断容器中的bean注册情况,也可以给容器中注册bean
boolean definition = registry.containsBeanDefinition("person");
if(property.contains("linux")){
return true;
}
return false;
}
写一个测试类,当前我们的环境是Windows10的环境,所以配置类中第二个显然是不满足的。
package com.atguigu.test;
import java.util.Map;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import com.atguigu.bean.Blue;
import com.atguigu.bean.Person;
import com.atguigu.config.MainConfig;
import com.atguigu.config.MainConfig2;
public class IOCTest {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);
@Test
public void test03(){
String[] namesForType = applicationContext.getBeanNamesForType(Person.class);
ConfigurableEnvironment environment = applicationContext.getEnvironment();
//动态获取环境变量的值;Windows 10
String property = environment.getProperty("os.name");
System.out.println(property);
for (String name : namesForType) {
System.out.println(name);
}
Map<String, Person> persons = applicationContext.getBeansOfType(Person.class);
System.out.println(persons);
}
}
打印出结果:
Windows 10 // 系统环境
bill // 第二个@Bean("linus")没有被注册进来
现在我们再改变一下系统环境

注意看虚拟机参数,这样我们的系统环境就会被识别为linux
点击Run运行,
linux // 系统环境
linus // linus被注册进来
需要注意的是:这个@Conditonal注解,也可以注解在类上,表示:类中组件统一设置。满足当前条件,这个类中配置的所有bean注册才能生效;
至于LinuxCondition和WindowsConditon这两个用来判断条件是否成立的类,可以参看文章开头给的链接。
四、Spring中使用@Conditional按照条件注册Bean的更多相关文章
- Spring注解驱动开发03(按照条件注册bean)
按照条件注册bean 使用@Conditional注解来控制bean的注册 使用步骤 先实现Condition接口,条件写在matches方法里 注意事项:Condition接口是org.spring ...
- 7、组件注册-@Conditional-按照条件注册bean
7.组件注册-@Conditional-按照条件注册bean @Conditional 按照一定的条件进行判断,满足条件给容器注入bean 按照条件进行动态装配. Spring 4 开始提供的一个注解 ...
- 使用spring配置类代替xml配置文件注册bean类
spring配置类,即在类上加@Configuration注解,使用这种配置类来注册bean,效果与xml文件是完全一样的,只是创建springIOC容器的方式不同: //通过xml文件创建sprin ...
- Spring系列11:@ComponentScan批量注册bean
回顾 在前面的章节,我们介绍了@Comfiguration和@Bean结合AnnotationConfigApplicationContext零xml配置文件使用Spring容器的方式,也介绍了通过& ...
- Spring 5:以函数式方式注册 Bean
http://www.baeldung.com/spring-5-functional-beans 作者:Loredana Crusoveanu 译者:http://oopsguy.com 1.概述 ...
- 【归纳】springboot中的IOC注解:注册bean和使用bean
目前了解的springboot中IOC注解主要分为两类: 1. 注册bean:@Component和@Repository.@Service.@Controller .@Configuration 共 ...
- Spring(五)核心容器 - 注册 Bean、BeanDefinitionRegistry 简介
目录 前言 正文 1.BeanDefinitionRegistry 简介 2.registerBeanDefinition 方法注册 Bean 最后 前言 上篇文章我们对 BeanDefinition ...
- 【学习笔记】Spring中的BeanFactory和ApplicationContext 以及 Bean的生命周期(Y2-3-2)
BeanFactory和ApplicationContext Spring的IoC容器就是一个实现了BeanFactory接口的可实例化类. Spring提供了两种不同的容器: 一种是最基本的Bean ...
- Spring IOC(2)----如何注册bean定义
前面说到IOC容器在刷新之前的一些初始化工作,现在来看看在refresh()方法中,是怎样来加载注册我们自己的bean定义的. refresh()方法中有很多功能,从注释中就可以看出来 我们本次重点关 ...
随机推荐
- 超详细的Hadoop2配置详解
1. 集群环境 Master 192.168.2.100 Slave1 192.168.2.101 Slave2 192.168.2.102 2. 下载安装包 Master wget http://m ...
- Intellij IDEA 与 Gitlab 实现代码上传与下载
整体流程:1.download project2.deposit project structure and set default server3.configure tomcat 2 steps4 ...
- css实现硬件加速
原文请点击一下链接: http://blog.teamtreehouse.com/increase-your-sites-performance-with-hardware-accelerated-c ...
- Spring源码窥探之:AOP注解
AOP也就是我们日常说的@面向切面编程,看概念比较晦涩难懂,难懂的是设计理念,以及这样设计的好处是什么.在Spring的AOP中,常用的几个注解如下:@Aspect,@Before,@After,@A ...
- [Codeforces 1251F]Red-White Fence
Description 题库链接 给你 \(n\) 块白木板,\(k\) 块红木板,分别有各自的长度 \(h_i\).让你用这些木板组成一段围栏,要满足: 只用一块红木板,且所有白木板的长度均严格小于 ...
- mac系统下 PHPStorm 快捷键
PHPStorm可以自己设置快捷键 按住command + , 打开Preferences点击Keymap,右边出现下拉框点击下拉框选择你想要的快捷键设置,eclipse快捷键比较常用 eclipse ...
- 关于Windows系统里的事后调试
我一直在想,应用程序抛出未处理的异常和附加到进程的调试器之间会发生什么.显然这些信息就在我眼皮底下,但我是瞎子.Windows调试器关于事后调试的文档包含了您想要知道的所有详细信息. 最常见的应用程序 ...
- WinDbg常用命令系列---单步执行p*
p (Step) p命令执行单个指令或源代码行,并可选地显示所有寄存器和标志的结果值.当子例程调用或中断发生时,它们被视为单个步骤. 用户模式: [~Thread] p[r] [= StartAddr ...
- zabbix值显示的问题
虽然在创建监控项的时候,是可以选值类型的,目前有的是整型,浮点型,日志,文本,字符串.但是不要误认为zabbix采集数据的时候就是按照这个格式采集的. zabbix各种接口采集到的数据都是字符串类型, ...
- 解决configure: WARNING: You will need re2c 0.13.4 or later
我在安装rabbitmq php扩展的时候发现 configure: WARNING: You will need re2c 0.13.4 or later if you want to regene ...