spring注解第03课 按条件加载Bean @Conditional
package com.atguigu.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; import com.atguigu.bean.Person;
import com.atguigu.condition.LinuxCondition;
import com.atguigu.condition.WindowsCondition;
import com.atguigu.test.IOCTest; //类中组件统一设置。满足当前条件,这个类中配置的所有bean注册都能生效;
@Conditional({WindowsCondition.class})
@Configuration
public class MainConfig2 {
/**
* @Conditional({Condition}) : 按照一定的条件进行判断,满足条件给容器中注册bean
*
* 如果系统是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);
}
}
2.条件1
package com.atguigu.condition; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata; //判断是否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;
} }
3.条件2
package com.atguigu.condition; import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata; //判断是否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;
} }
spring注解第03课 按条件加载Bean @Conditional的更多相关文章
- 【Spring注解驱动开发】使用@PropertySource加载配置文件,我只看这一篇!!
写在前面 很多小伙伴都在问:冰河,你的Spring专题更新完了吗?怎么感觉像是写了一半啊?我:没有更新完呀,整个专题预计会有70多篇.那怎么更新了一半就去写别的了呢?那是因为有很多其他的小伙伴在后台留 ...
- Spring多种加载Bean方式简析
1 定义bean的方式 常见的定义Bean的方式有: 通过xml的方式,例如: <bean id="dictionaryRelMap" class="java.ut ...
- 【Spring源码分析】非懒加载的单例Bean初始化过程(下篇)
doCreateBean方法 上文[Spring源码分析]非懒加载的单例Bean初始化过程(上篇),分析了单例的Bean初始化流程,并跟踪代码进入了主流程,看到了Bean是如何被实例化出来的.先贴一下 ...
- Spring源码分析:非懒加载的单例Bean初始化过程(下)
上文Spring源码分析:非懒加载的单例Bean初始化过程(上),分析了单例的Bean初始化流程,并跟踪代码进入了主流程,看到了Bean是如何被实例化出来的.先贴一下AbstractAutowireC ...
- spring加载bean流程解析
spring作为目前我们开发的基础框架,每天的开发工作基本和他形影不离,作为管理bean的最经典.优秀的框架,它的复杂程度往往令人望而却步.不过作为朝夕相处的框架,我们必须得明白一个问题就是sprin ...
- Bean 注解(Annotation)配置(1)- 通过注解加载Bean
Spring 系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of ...
- 【Spring源码分析】非懒加载的单例Bean初始化过程(上篇)
代码入口 上文[Spring源码分析]Bean加载流程概览,比较详细地分析了Spring上下文加载的代码入口,并且在AbstractApplicationContext的refresh方法中,点出了f ...
- 【Spring源码分析】非懒加载的单例Bean初始化前后的一些操作
前言 之前两篇文章[Spring源码分析]非懒加载的单例Bean初始化过程(上篇)和[Spring源码分析]非懒加载的单例Bean初始化过程(下篇)比较详细地分析了非懒加载的单例Bean的初始化过程, ...
- 【死磕 Spring】----- IOC 之 加载 Bean
原文出自:http://cmsblogs.com 先看一段熟悉的代码: ClassPathResource resource = new ClassPathResource("bean.xm ...
随机推荐
- 【mysql】mysql索引及优化学习
一般优化mysql首先看查找的数据有没有用到索引,没有索引就加索引,有索引时候避免索引失效.(如果优化器觉得不需要索引就能返回所需要的数据暂不考虑) 看下面两条语句 MySQL [release_te ...
- smtp
新闻系统的定时通知初步有三种实用方式,1.短信 2.邮箱 3.微信 短信就不得不使用第三方平台,虽说5分一条,但耐不住量大,一天1000条的话,50元也是一笔不小的支出. 这时,邮箱和微信的优势就体现 ...
- centos7/RHEL7最小化系统安装gnome图形界面
应用场景:对于比较熟悉linux系统的用户来说,全命令行系统可能来的比较简单明了高效,也存在某些情况下需要有像winodws下弹出对话框的情形需求,或者对于初识linux习惯windows界面的用户来 ...
- bzoj3467: Crash和陶陶的游戏
就一篇题解: BZOJ3467 : Crash和陶陶的游戏 - weixin_34248487的博客 - CSDN博客 1.离线,建出Atrie树:B树的倍增哈希数组,节点按照到根路径字典序排序 2. ...
- poj2689 Prime Distance
题意:求[a, b]之间差最大/小的相邻素数. 0 < a, b < 2^32, 0 < b - a <= 1e6 首先发现a,b很大,以至于无法求出素数来. 然后就考虑退而求 ...
- A1003. Emergency
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- “adb不是内部或外部命令,也不是可运行的程序或批量文件“
首先这个问题有两种可能: 1.就是没有配置环境变量, 这个只需要将android安装:例如C:\Program File\android-sdk-windows\tools加入到 系统变量Path中, ...
- 第五篇-以显式意图(Explicit Intent)跳往其它Activity
此项目基于第四篇. Intent(意图) Explicit Intent(显式意图): 清楚指明需要前往的Activity的名称 用于APP内部的连接 Inplicit Intent(隐式意图): 不 ...
- P2602 [ZJOI2010]数字计数
https://www.luogu.org/problemnew/show/P2602 数位dp #include <bits/stdc++.h> using namespace std; ...
- pcntl_exec()
<?php /******************************* *查看phpinfo编译参数--enable-pcntl *作者 Spider *nc -vvlp 443 **** ...