四、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()方法中有很多功能,从注释中就可以看出来 我们本次重点关 ...
随机推荐
- NOIP 2018 考前须知
Day0Day0Day0来水一发 Created with Raphaël 2.2.0开始考试浏览题面(3遍),注意数据范围初步判定难度,先易后难15分钟左右想正解实在想吃不出写暴力,NOIP部分分很 ...
- noip初赛试题
链接: https://pan.baidu.com/s/1yoOMIUqMRBnBUPprC3o6HQ&shfl=shareset 提取码: m8ns 复制这段内容后打开百度网盘手机App,操 ...
- php正则表示中的元字符
元字符 抛出问题: \d 代表匹配一个字符.而我现在想要匹配十个八个,任意多个数字肿么办? 这个时候我们就要用到元字符.在使用原子的时候,发现只能够匹配一个字符,可是要匹配多个字符就出现了问题.大理石 ...
- Dump文件定制工具---MiniDump Wizard
MiniDump向导应用程序允许在不编写代码的情况下尝试MiniDumpWriteDump和MiniDumpCallback函数.可以指定将传递给MiniDumpWriteDump函数的MINIDUM ...
- 洛谷 P3380 【模板】二逼平衡树(树套树)-线段树套splay
P3380 [模板]二逼平衡树(树套树) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上的数 ...
- 算法:贪心、回溯(su)、分治、动态规划,思想简要
贪心算法: 只做出当前看来最好的选择,而不从整体考虑最优,他所作出的是局部最优解.使用该算法的前提是必须具备无后效性,即某个状态以前的选择不会影响以后的状态的选择,只与当前状态有关. 回溯算法: 本质 ...
- JVM内存的划分
JVM内存的划分有五片: 1. 寄存器: 2. 本地方法区: 3. 方法区: 4. 栈内存: 5. 堆内存.
- TRUNCATE、Drop、Delete 的用法
//Truncate是一个能够快速清空资料表内所有资料的SQL语法.并且能针对具有自动递增值的字段,做计数重置归零重新计算的作用. truncate table1; drop table1; dele ...
- IDEA中设置自动build-改动代码,不用重启工程,刷新页面即可
1.CTRL + SHIFT + A --> 查找Registry --> 找到并勾选compiler.automake.allow.when.app.running 2. FILE ...
- Linux 文件压缩、打包
文件压缩 计算机使用byte单位来计量.实际上,计算机最小的计量单位是bit.1byte = 8 bit.如果记录1这个数字,00000001,1会在最右边占一个1个bit 其他7个bit会被填上0. ...