当前位置: 首页 > news >正文

做购物网站哪家公司好/北京seo收费

做购物网站哪家公司好,北京seo收费,做图书馆网站模板,合肥网站建设zgkr文章目录1 Conditional注解的作用2 学习dome3 总结本博客demo源码地址 https://github.com/suchahaerkang/spring-annotation.git 1 Conditional注解的作用 Conditional是spring4中的一个注解,作用是在spring容器中注册组件的时候,进行一些条件判断&am…

文章目录

  • 1 @Conditional注解的作用
  • 2 学习dome
  • 3 总结

本博客demo源码地址
https://github.com/suchahaerkang/spring-annotation.git

1 @Conditional注解的作用

@Conditional是spring4中的一个注解,作用是在spring容器中注册组件的时候,进行一些条件判断,如果判断成功,那么就可以将组件注册在spring容器中去,否则不注册,@Conditional注解在spring源码中用到了很多,所以这个注解是非常重要的,我们很有必要去掌握。

2 学习dome

为了方便学习,先描述一个小的场景,我们都知道windows之父是bill gates, linux之父是linus,现在我们将这两个人注册都spring容器中

    @Bean("bill")public Person person01(){return new Person("bill", 65);}@Bean("linus")public Person person02(){return new Person("linus", 67);}

写个测试用例,看一下容器中是否有这两个实例了

 	@Testpublic void test04(){//创建容器ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);//通过组件类型从容器中获取所有的实例名字String[] beanNames = applicationContext.getBeanNamesForType(Person.class);for (String name :beanNames) {System.out.println(name);}}

运行结果
在这里插入图片描述
然后现在我们有个需求,希望程序在windows环境下,只能将bill这个实例注册到容器中去,在linux环境下将linus这个实例注册到环境中去,下面我们就通过@Conditional这个注解来解决这个需求

因为@Conditional注解需要一个或则多个条件,所以我们先编写两个条件,条件必须实现Condition 这个接口。这个两个条件分别是:
如果程序在windows操作系统下,那么就返回true

	public class WindowsCondition implements Condition {/*** @description:* @param conditionContext 判断条件需要的上下文* @param annotatedTypeMetadata 获取的注解信息* @return: boolean* @author: sukang* @date: 2020/3/5 12:28*/public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {//可以获取ioc容器创建实例的bean工厂ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory();//可以获取类加载器ClassLoader classLoader = conditionContext.getClassLoader();//可以获取运行时的环境变量和jvm变量Environment environment = conditionContext.getEnvironment();//可以获取注册都容器中的所有实例BeanDefinitionRegistry beanDefinitionRegistry = conditionContext.getRegistry();//这里我们就通过Environment对象获取操作系统的名字String osName = environment.getProperty("os.name");System.out.println("本操作系统为:" + osName);//如果操作系统为windows那么返回成功if(osName.contains("Windows")){return true;}return false;}}

如果操作系统为Linux,那么就返回为true

	/*** @description:* @author: sukang* @date: 2020-03-05 12:38*/public class LinuxCondition implements Condition {public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {//可以获取运行时的环境变量和jvm变量Environment environment = conditionContext.getEnvironment();//这里我们就通过Environment对象获取操作系统的名字String osName = environment.getProperty("os.name");System.out.println("本操作系统为:" + osName);//如果操作系统为windows那么返回成功if(osName.contains("Linux")){return true;}return false;}}

配置文件上加上@Conditional的注解

  	@Conditional(WindowsCondition.class)@Bean("bill")public Person person01(){return new Person("bill", 65);}@Conditional(LinuxCondition.class)@Bean("linus")public Person person02(){return new Person("linus", 67);}

测试用例

 	@Testpublic void test04(){//创建容器ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig2.class);//通过组件类型从容器中获取所有的实例名字String[] beanNames = applicationContext.getBeanNamesForType(Person.class);for (String name :beanNames) {System.out.println(name);}}

开始测试,本机为windows环境,所以运行结果为
在这里插入图片描述
为了模拟程序运行在linux环境下,我们在idea改一下运行环境
在这里插入图片描述
在这里插入图片描述
运行结果
在这里插入图片描述

3 总结

@Conditional注解可以放在方法或则类上面,如果放在类上表示如果条件不满足,那么配置类里面的组件都不能注册到容器中去

http://www.lbrq.cn/news/1591111.html

相关文章:

  • mac做网站/商业计划书
  • 网络文化经营许可证申请/seo外链友情链接
  • 专业做商铺的网站/百度指数工具
  • 影视播放网站建设/免费网站大全下载
  • 我国政府门户网站的建设情况/关键词指数查询
  • 美橙网站注册/网上商城建设
  • vue开发自适应网站/长春关键词优化排名
  • 长沙网站免费建站/青岛百度整站优化服务
  • 哈尔滨网站设计公司/个人网页在线制作
  • 举例说明/站外seo推广
  • 网站建设 软文/seo引擎
  • wordpress汉字/seo推广培训课程
  • 福建网站开发企业/网站查找工具
  • 怎样推广公司的网站/疫情防控最新政策
  • 怎么用模板建网站/百度推广登录入口
  • 无锡公共工程建设中心网站/整站seo免费咨询
  • 装修客户资源在哪里找/搜索引擎优化的各种方法
  • 中山市政府网站建设/营销策略范文
  • 开源cms建站/seo优化报告
  • 网站版面做的很好的公司/网站外链平台
  • wordpress在线监测/免费seo诊断
  • 宁夏自治区住房城乡建设厅网站/google chrome官网
  • wordpress添加一级菜单/seo外包公司优化
  • 微网站平台微网站建设方案模板/上海营销公司
  • 网站规划与开发技术/济南网站万词优化
  • 做网站送企业邮箱/全网推广成功再收费
  • 百度上的网站怎么做/免费推广app软件下载
  • php网站开发培训/成都seo网络优化公司
  • 山东泰安疫情/兰州网络推广优化怎样
  • 北京企业建设网站公司哪家好/个人博客网站模板
  • 【R语言】 高清美观的 MaxEnt 刀切图(Jackknife)绘制——提升论文质量
  • C语言的数组与字符串练习题1
  • 简单spring boot项目,之前练习的,现在好像没有达到效果
  • 阿里云 Flink
  • Fabric.js从入门学习到实现labelImg矩形多边形标注工具【上】
  • 探索设计模式的宝库:Java-Design-Patterns