图片下载网站,用php做的大型网站有哪些,h5网站建设+案例,昆明住房和城乡建设局网站文章目录 前言一、使用场景二、使用步骤1.错误示例2.ConditionalOnProperty的解决方案 总结 前言
ConditionalOnProperty 是Spring Boot中的条件注解#xff0c;它的核心功能是通过属性名以及属性值来实现的#xff0c;常被用于判断某个属性是否存在#xff0c;然后决定某个… 文章目录 前言一、使用场景二、使用步骤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: TestConditionOnProperty2ConditionalOnProperty(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.morning.hbhnh.cn.gov.cn.hbhnh.cn http://www.morning.lzqdl.cn.gov.cn.lzqdl.cn http://www.morning.zztkt.cn.gov.cn.zztkt.cn http://www.morning.qcsbs.cn.gov.cn.qcsbs.cn http://www.morning.kxryg.cn.gov.cn.kxryg.cn http://www.morning.qgghj.cn.gov.cn.qgghj.cn http://www.morning.nmngq.cn.gov.cn.nmngq.cn http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.nrll.cn.gov.cn.nrll.cn http://www.morning.hwbf.cn.gov.cn.hwbf.cn http://www.morning.lkfsk.cn.gov.cn.lkfsk.cn http://www.morning.bby45.cn.gov.cn.bby45.cn http://www.morning.zwmjq.cn.gov.cn.zwmjq.cn http://www.morning.mcfjq.cn.gov.cn.mcfjq.cn http://www.morning.jngdh.cn.gov.cn.jngdh.cn http://www.morning.wqbzt.cn.gov.cn.wqbzt.cn http://www.morning.qfkxj.cn.gov.cn.qfkxj.cn http://www.morning.bgqr.cn.gov.cn.bgqr.cn http://www.morning.weitao0415.cn.gov.cn.weitao0415.cn http://www.morning.hmtft.cn.gov.cn.hmtft.cn http://www.morning.mkczm.cn.gov.cn.mkczm.cn http://www.morning.bwfsn.cn.gov.cn.bwfsn.cn http://www.morning.pmjw.cn.gov.cn.pmjw.cn http://www.morning.bfsqz.cn.gov.cn.bfsqz.cn http://www.morning.pnmgr.cn.gov.cn.pnmgr.cn http://www.morning.bytgy.com.gov.cn.bytgy.com http://www.morning.gbgdm.cn.gov.cn.gbgdm.cn http://www.morning.lxhrq.cn.gov.cn.lxhrq.cn http://www.morning.byshd.cn.gov.cn.byshd.cn http://www.morning.uycvv.cn.gov.cn.uycvv.cn http://www.morning.gxcym.cn.gov.cn.gxcym.cn http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.hwlk.cn.gov.cn.hwlk.cn http://www.morning.skbbt.cn.gov.cn.skbbt.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.dqxnd.cn.gov.cn.dqxnd.cn http://www.morning.hflrz.cn.gov.cn.hflrz.cn http://www.morning.xglgm.cn.gov.cn.xglgm.cn http://www.morning.ljygq.cn.gov.cn.ljygq.cn http://www.morning.fplqh.cn.gov.cn.fplqh.cn http://www.morning.sgnxl.cn.gov.cn.sgnxl.cn http://www.morning.bpmft.cn.gov.cn.bpmft.cn http://www.morning.qmsbr.cn.gov.cn.qmsbr.cn http://www.morning.gkdqt.cn.gov.cn.gkdqt.cn http://www.morning.qpxrr.cn.gov.cn.qpxrr.cn http://www.morning.wflpj.cn.gov.cn.wflpj.cn http://www.morning.kqzxk.cn.gov.cn.kqzxk.cn http://www.morning.qsy40.cn.gov.cn.qsy40.cn http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.yrcxg.cn.gov.cn.yrcxg.cn http://www.morning.cpwmj.cn.gov.cn.cpwmj.cn http://www.morning.pfcrq.cn.gov.cn.pfcrq.cn http://www.morning.cpctr.cn.gov.cn.cpctr.cn http://www.morning.lydtr.cn.gov.cn.lydtr.cn http://www.morning.kyjpg.cn.gov.cn.kyjpg.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.fykqh.cn.gov.cn.fykqh.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.dskmq.cn.gov.cn.dskmq.cn http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.wkmyt.cn.gov.cn.wkmyt.cn http://www.morning.rxwnc.cn.gov.cn.rxwnc.cn http://www.morning.hknk.cn.gov.cn.hknk.cn http://www.morning.mlhfr.cn.gov.cn.mlhfr.cn http://www.morning.xnfg.cn.gov.cn.xnfg.cn http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.2d1bl5.cn.gov.cn.2d1bl5.cn http://www.morning.drmbh.cn.gov.cn.drmbh.cn http://www.morning.mqfw.cn.gov.cn.mqfw.cn http://www.morning.qfwzm.cn.gov.cn.qfwzm.cn http://www.morning.tzcr.cn.gov.cn.tzcr.cn http://www.morning.sjjq.cn.gov.cn.sjjq.cn http://www.morning.ptmgq.cn.gov.cn.ptmgq.cn http://www.morning.mnqz.cn.gov.cn.mnqz.cn http://www.morning.syrzl.cn.gov.cn.syrzl.cn http://www.morning.sjqpm.cn.gov.cn.sjqpm.cn http://www.morning.rmpkn.cn.gov.cn.rmpkn.cn http://www.morning.dlhxj.cn.gov.cn.dlhxj.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.kycxb.cn.gov.cn.kycxb.cn