@ConditionalOnProperty 用法

2023-10-12 18:05

本文主要是介绍@ConditionalOnProperty 用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前言
  • 一、使用场景
  • 二、使用步骤
    • 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概述

这篇关于@ConditionalOnProperty 用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/197531

相关文章

oracle中exists和not exists用法举例详解

《oracle中exists和notexists用法举例详解》:本文主要介绍oracle中exists和notexists用法的相关资料,EXISTS用于检测子查询是否返回任何行,而NOTE... 目录基本概念:举例语法pub_name总结 exists (sql 返回结果集为真)not exists (s

Springboot中Jackson用法详解

《Springboot中Jackson用法详解》Springboot自带默认json解析Jackson,可以在不引入其他json解析包情况下,解析json字段,下面我们就来聊聊Springboot中J... 目录前言Jackson用法将对象解析为json字符串将json解析为对象将json文件转换为json

bytes.split的用法和注意事项

当然,我很乐意详细介绍 bytes.Split 的用法和注意事项。这个函数是 Go 标准库中 bytes 包的一个重要组成部分,用于分割字节切片。 基本用法 bytes.Split 的函数签名如下: func Split(s, sep []byte) [][]byte s 是要分割的字节切片sep 是用作分隔符的字节切片返回值是一个二维字节切片,包含分割后的结果 基本使用示例: pa

UVM:callback机制的意义和用法

1. 作用         Callback机制在UVM验证平台,最大用处就是为了提高验证平台的可重用性。在不创建复杂的OOP层次结构前提下,针对组件中的某些行为,在其之前后之后,内置一些函数,增加或者修改UVM组件的操作,增加新的功能,从而实现一个环境多个用例。此外还可以通过Callback机制构建异常的测试用例。 2. 使用步骤         (1)在UVM组件中内嵌callback函

这些ES6用法你都会吗?

一 关于取值 取值在程序中非常常见,比如从对象obj中取值 const obj = {a:1b:2c:3d:4} 吐槽: const a = obj.a;const b = obj.b;const c = obj.c;//或者const f = obj.a + obj.b;const g = obj.c + obj.d; 改进:用ES6解构赋值

2021-8-14 react笔记-2 创建组件 基本用法

1、目录解析 public中的index.html为入口文件 src目录中文件很乱,先整理文件夹。 新建components 放组件 新建assets放资源   ->/images      ->/css 把乱的文件放进去  修改App.js 根组件和index.js入口文件中的引入路径 2、新建组件 在components文件夹中新建[Name].js文件 //组件名首字母大写

Cmake之3.0版本重要特性及用法实例(十三)

简介: CSDN博客专家、《Android系统多媒体进阶实战》一书作者 新书发布:《Android系统多媒体进阶实战》🚀 优质专栏: Audio工程师进阶系列【原创干货持续更新中……】🚀 优质专栏: 多媒体系统工程师系列【原创干货持续更新中……】🚀 优质视频课程:AAOS车载系统+AOSP14系统攻城狮入门视频实战课 🚀 人生格言: 人生从来没有捷径,只有行动才是治疗恐惧

关于断言的部分用法

1、带变量的断言  systemVerilog assertion 中variable delay的使用,##[variable],带变量的延时(可变延时)_assertion中的延时-CSDN博客 2、until 的使用 systemVerilog assertion 中until的使用_verilog until-CSDN博客 3、throughout的使用   常用于断言和假设中的

ExpandableListView的基本用法

QQ上的好友列表在Android怎么实现,有一个最简单的方法,那就是ExpandableListView,下面简单介绍一下ExpandableListview的用法。 先看看效果图,没有找到大小合适的图片,所以凑合着看吧。     一、准备工作(界面,和需要的数据)             <? xml   version = "1.0"   encoding =

Hbase 查询相关用法

Hbase 查询相关用法 public static void main(String[] args) throws IOException {//Scan类常用方法说明//指定需要的family或column ,如果没有调用任何addFamily或Column,会返回所有的columns; // scan.addFamily(); // scan.addColumn();// scan.se