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 ...
随机推荐
- linux开发板出现Read-only file system的解决办法
@2018-11-29 创建文件夹出现如下提示 mkdir: can't create directory 'test': Read-only file system 使用命令 mount rw -o ...
- 按奇偶排序数组 II
题目描述 给定一个非负整数数组 A, A 中一半整数是奇数,一半整数是偶数. 对数组进行排序,以便当 A[i] 为奇数时,i 也是奇数:当 A[i] 为偶数时, i 也是偶数. 你可以返回任何满足上述 ...
- 这些保护Spring Boot 应用的方法,你都用了吗?
这些保护Spring Boot 应用的方法,你都用了吗? 生如夏花 SpringForAll社区 今天 Spring Boot大大简化了Spring应用程序的开发.它的自动配置和启动依赖大大减少了开始 ...
- linux free命令
Linux上的free命令详解 free命令的所有输出值都是从/proc/meminfo中读出的 total used free shared buffers cached Mem: -/+ buff ...
- Win32 SDK:ListBox 为什么不整个 LB_SETTEXT
Win32 & SDK: ListBox 有个 LB_GETTEXT,为什么没有 LB_SETTEXT,想修改指定Item的Text,还真不容易. 自已写了一个,支持单选和多选模式,部分代码: ...
- django框架中的全文检索Haystack
1.什么是Haystack Haystack是django的开源全文搜索框架(全文检索不同于特定字段的模糊查询,使用全文检索的效率更高 ),该框架支持Solr,Elasticsearch,Whoosh ...
- 把本人基于Dubbo的毕业设计分享粗来~
这个毕业设计不是太让我满意,可能时间关系吧,但是现在工作了我也懒得没时间去完善了[目前也就有一个功能没完成,就是给产品经理送绿帽子的模块] 项目地址:GitHub 技术栈:Spring+SpringM ...
- toString()和toLocaleString()有什么区别
偶然之间用到这两个方法 然后在数字转换成字符串的时候,并没有感觉这两个方法有什么区别,如下: 1 2 3 4 5 6 7 8 var e=123 e.toString() "123& ...
- Qt ------ 添加某个功能,比如(QSerialPort)注意事项
添加某个功能,比如(QSerialPort),不仅需要保护头文件 还需要在.pro文件内添加 QT += serialport
- icmpsh解决运行报错
运行./run.sh报错 sysctl -w net.ipv4.icmp_echo_ignore_all=1 >/dev/null python icmpsh_m.py 攻击机ip 目标机ip ...