四、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()方法中有很多功能,从注释中就可以看出来 我们本次重点关 ...
随机推荐
- Java学习 从0.1开始(一)
写在前面: 之前从事过.NET,C,C++相关的开发,Java是一直没有学习的新领域.最近,应工作需要,开始学习Java相关的知识.又因为新公司并没有完整的系统架构,所以学习方向会侧重架构方向(Cod ...
- 全球百大最有前景AI公司出炉,中国成独角兽最强诞生地
https://new.qq.com/omn/20190210/20190210B0BVK2.html 硅谷最强智库之一的 CB Insights 日前发布 AI 100 2019 报告,在这 100 ...
- CDN工作机制
CDN(content delivery network),即内容分布网络,是一种构建在现有Internet上的一种先进的流量分配网络.CDN以缓存网站中的静态数据为主,当用户请求动态内容时,先从CD ...
- navicat设置唯一
https://blog.csdn.net/Song_JiangTao/article/details/82192189
- java 值传递、引用传递
class Demo02 { public static void main(String[] args) { int a=1; get(a);//值传递 System.out.println(a); ...
- 前端微信小程序仿菜谱精灵
需求描述及交互分析 设计思路和相关知识点 底部标签导航设计 幻灯片轮播效果设计 菜谱专题列表显示设计 菜谱专题详情设计 菜谱分类设计 幻灯片轮播效果动态切换展示一些美食图片 若本号内容有做得不到位的地 ...
- GoCN每日新闻(2019-10-07)
GoCN每日新闻(2019-10-07) 国庆专辑:GopherChina 祝大家国庆节快乐(假期最后一天) GoCN每日新闻(2019-10-07) 1. Go 不好的点:JSON 解析的探 ...
- lnmp环境脚本快速搭建
进入lnmp官网 https://lnmp.org/download.html 如图: 进入Linux服务器并执行wget命令下载 wget http://soft.vpser.net/lnmp/ln ...
- 第10组 Alpha冲刺(4/6)
链接部分 队名:女生都队 组长博客: 博客链接 作业博客:博客链接 小组内容 恩泽(组长) 过去两天完成了哪些任务 描述 学习调用中国天气网API,接近实现天气推送功能 对天气推送的形式进行讨论及重确 ...
- 进程、线程、IP、端口间关系
进程.线程.IP.端口间关系 进程是指在系统中正在运行的一个应用程序: 线程是系统分配处理器时间资源的基本单元,或者说进程之内独立执行的一个单元. 对于操作系统而言,其调度单元是线程.一个进程至少包括 ...