@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

相关文章

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

Python中yield的用法和实际应用示例

《Python中yield的用法和实际应用示例》在Python中,yield关键字主要用于生成器函数(generatorfunctions)中,其目的是使函数能够像迭代器一样工作,即可以被遍历,但不会... 目录python中yield的用法详解一、引言二、yield的基本用法1、yield与生成器2、yi

深度解析Python yfinance的核心功能和高级用法

《深度解析Pythonyfinance的核心功能和高级用法》yfinance是一个功能强大且易于使用的Python库,用于从YahooFinance获取金融数据,本教程将深入探讨yfinance的核... 目录yfinance 深度解析教程 (python)1. 简介与安装1.1 什么是 yfinance?

Python库 Django 的简介、安装、用法入门教程

《Python库Django的简介、安装、用法入门教程》Django是Python最流行的Web框架之一,它帮助开发者快速、高效地构建功能强大的Web应用程序,接下来我们将从简介、安装到用法详解,... 目录一、Django 简介 二、Django 的安装教程 1. 创建虚拟环境2. 安装Django三、创

python中update()函数的用法和一些例子

《python中update()函数的用法和一些例子》update()方法是字典对象的方法,用于将一个字典中的键值对更新到另一个字典中,:本文主要介绍python中update()函数的用法和一些... 目录前言用法注意事项示例示例 1: 使用另一个字典来更新示例 2: 使用可迭代对象来更新示例 3: 使用

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Python中的sort()和sorted()用法示例解析

《Python中的sort()和sorted()用法示例解析》本文给大家介绍Python中list.sort()和sorted()的使用区别,详细介绍其参数功能及Timsort排序算法特性,涵盖自适应... 目录一、list.sort()参数说明常用内置函数基本用法示例自定义函数示例lambda表达式示例o

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

MySQL ORDER BY 语句常见用法、示例详解

《MySQLORDERBY语句常见用法、示例详解》ORDERBY是结构化查询语言(SQL)中的关键字,隶属于SELECT语句的子句结构,用于对查询结果集按指定列进行排序,本文给大家介绍MySQL... 目录mysql ORDER BY 语句详细说明1.基本语法2.排序方向详解3.多列排序4.常见用法示例5.

DNS查询的利器! linux的dig命令基本用法详解

《DNS查询的利器!linux的dig命令基本用法详解》dig命令可以查询各种类型DNS记录信息,下面我们将通过实际示例和dig命令常用参数来详细说明如何使用dig实用程序... dig(Domain Information Groper)是一款功能强大的 linux 命令行实用程序,通过查询名称服务器并输