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

图片下载网站苏州建网站的公

图片下载网站,苏州建网站的公,采集wordpress上的内容,网页制作免费网站制作文章目录 前言一、使用场景二、使用步骤1.错误示例2.ConditionalOnProperty的解决方案 总结 前言 ConditionalOnProperty 是Spring Boot中的条件注解,它的核心功能是通过属性名以及属性值来实现的,常被用于判断某个属性是否存在,然后决定某个…

文章目录

  • 前言
  • 一、使用场景
  • 二、使用步骤
    • 1.错误示例
    • 2.@ConditionalOnProperty的解决方案
  • 总结


前言

@ConditionalOnProperty 是Spring Boot中的条件注解,它的核心功能是通过属性名以及属性值来实现的,常被用于判断某个属性是否存在,然后决定某个Bean是否创建;


一、使用场景

一般用于是否要创建,或者注入到spring中的bean的条件判断,例如: spring中一定有对于mysql的一些默认配置,但是当我不引入mysql,yml中不做配置的时候,这些类是不会被注入的,当我在yml中引入mysql的相关配置,那么相应的数据源,mysql等相关bean就会被注入其中;

二、使用步骤

这里举个例子,也是项目中常见的问题,当一个实现类有两个实现类的时候,我们注入使用的时候会有问题;

1.错误示例

  • 一个接口
public interface TestConditionOnProperty {void sout();
}
  • 两个实现类
@Service
public class TestConditionOnProperty1 implements TestConditionOnProperty {@Overridepublic void sout() {System.out.println(">>>>>>>>>>>>>> TestConditionOnProperty1 <<<<<<<<<<<<");}
}@Service
public class TestConditionOnProperty2 implements TestConditionOnProperty {@Overridepublic void sout() {System.out.println(">>>>>>>>>>>>>> TestConditionOnProperty2 <<<<<<<<<<<<");}
}
  • 开始测试, 问题出现
@SpringBootTest
class TestConditionOnPropertyTest {@AutowiredTestConditionOnProperty testConditionOnProperty;@Testvoid sout() {testConditionOnProperty.sout();}
}

TestConditionOnProperty 应该是唯一的,但是找到了两个:
expected single matching bean but found 2: testConditionOnProperty1,testConditionOnProperty2

  • 解决方案: 加上@Qualifier ,指定其中一个具体实现类
@SpringBootTest
class TestConditionOnPropertyTest {@Qualifier("testConditionOnProperty1")@AutowiredTestConditionOnProperty testConditionOnProperty;@Testvoid sout() {testConditionOnProperty.sout();}
}

2.@ConditionalOnProperty的解决方案

  • 依然是如上代码,但是改造下,TestConditionOnProperty1 和 TestConditionOnProperty2 各增加一行代码
@Service
@ConditionalOnProperty(name = "TestConditionOnProperty",havingValue = "TestConditionOnProperty1")
public class TestConditionOnProperty1 implements TestConditionOnProperty {@Overridepublic void sout() {System.out.println(">>>>>>>>>>>>>> TestConditionOnProperty1 <<<<<<<<<<<<");}
}@Service
@ConditionalOnProperty(name = "TestConditionOnProperty",havingValue = "TestConditionOnProperty2")
public class TestConditionOnProperty2 implements TestConditionOnProperty {@Overridepublic void sout() {System.out.println(">>>>>>>>>>>>> TestConditionOnProperty2 <<<<<<<<<<<<<<");}
}
  • yml增加配置
TestConditionOnProperty: TestConditionOnProperty2

@ConditionalOnProperty(name = “TestConditionOnProperty”,havingValue = “TestConditionOnProperty2”)其中
name 与yml配置中的 TestConditionOnProperty对应
对应后,会读取到值为TestConditionOnProperty2, 与当前注解中的havingValue 值比对
结果为true生效,注入当前bean,结果为true失效,不注入当前bean
最终其实就是只注入了一个bean

  • 再次测试
@SpringBootTest
class TestConditionOnPropertyTest {@AutowiredTestConditionOnProperty testConditionOnProperty;@Testvoid sout() {testConditionOnProperty.sout();}
}

打印出第二个问题


总结

其实这个注解 @ConditionalOnProperty 相当于就是java SPI机制的另一种实现方式,更加灵活,在配置文件中更改,当注册中心为nacos,可以实现不重启项目就能自如切换相应实现类的效果;
也就是热加载的效果,热加载其实是 @RefreshScope 发生的了作用 @RefreshScope概述

http://www.yayakq.cn/news/389654/

相关文章:

  • 淄博网站制作方案北京最近发布会直播
  • 做网站的时候宽高上海缪斯设计公司的主题文化
  • 加快建设企业门户网站建软件开发服务开票税目
  • 国外能下载模板的网站展厅设计展览设计公司
  • 网站备案取消前置审批淘宝店铺怎么引流推广
  • 无极分期网站网站建设构想
  • 网站开发计划和预算seo课程培训机构
  • 免费网站建站申请张家口认证助手app
  • 如何建设万网网站陕西省建设监理协会官网站
  • 怎样将qq空间建设为个人网站宁波seo哪家好快速推广
  • 手机免费建站系统如东网站建设
  • 房产网站建设公司海外推广平台有哪些?
  • 怎么样才能自己做网站打广告网页设计的基本步骤和流程
  • 东莞网站建设公司服务平台外贸公司介绍范文
  • 网站建设数据库建设2015年做那些网站致富
  • 白帽seo公司seo搜索优化培训
  • 泉州模板自助建站广州域名企业网站建站哪家好
  • wordpress 中介网站wordpress免登录支付
  • 自己做的网页加在网站文章上为什么打不开济南电商代运营公司
  • 越秀五屏网站建设室内设计好的大学排名
  • 同一服务器如何建设多个网站哪里有网站开发设计
  • 宁波网站设计推广服务公司微信开发者工具手机版
  • 网站配色方案橙色用asp做网站登录页面
  • 做图书出版 外国网站为什么别的电脑能打开的网站我的电脑打不开
  • 怎样创建网站快捷方式正邦做网站多少钱
  • 龙华网站建设网站定制营销型网站建设的优缺点
  • 学校门户网站怎么做单位的网站建设费会计处理
  • 网站备案背景墙免费的制作网站
  • 织梦系统如何做网站建设学校网站前的需求分析
  • 我的南京网站wordpress列表无图像