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

SpringBoot条件装配注解

SpringBoot条件装配注解

Spring Boot 提供了一系列条件装配注解,用于控制 Bean 的创建和装配过程。以下是一些常用的条件装配注解及其详细介绍:
@ConditionalOnClass
作用:当类路径中存在指定的类时,才会创建该 Bean。
示例:

 @ConditionalOnClass(RedisTemplate.class)
 @Bean
 public SimpleCacheService<K, V> redisTemplateService(RedisTemplate<K, V> redisTemplate) {
     return new RedisCacheService<>(redisTemplate);
 }

说明:只有当类路径中存在 RedisTemplate 类时,才会创建 redisTemplateService Bean。

@ConditionalOnMissingClass
作用:当类路径中不存在指定的类时,才会创建该 Bean。
示例:

 @ConditionalOnMissingClass("org.springframework.data.redis.core.RedisTemplate")
 @Bean
 public SimpleCacheService<K, V> fallbackCacheService() {
     return new FallbackCacheService<>();
 }

说明:只有当类路径中不存在 RedisTemplate 类时,才会创建 fallbackCacheService Bean。

@ConditionalOnBean
作用:当容器中存在指定类型的 Bean 时,才会创建该 Bean。
示例:

 @ConditionalOnBean(RedisTemplate.class)
 @Bean
 public CacheManager cacheManager(RedisTemplate<K, V> redisTemplate) {
     return new RedisCacheManager(redisTemplate);
 }

说明:只有当容器中存在 RedisTemplate Bean 时,才会创建 cacheManager Bean。

@ConditionalOnMissingBean
作用:当容器中不存在指定类型的 Bean 时,才会创建该 Bean。
示例:

 @ConditionalOnMissingBean(SimpleCacheService.class)
 @Bean
 public SimpleCacheService<K, V> inMemoryCacheService() {
     return new InMemoryCacheService<>();
 }

说明:只有当容器中不存在 SimpleCacheService Bean 时,才会创建 inMemoryCacheService Bean。

@ConditionalOnProperty
作用:当配置文件中的某个属性满足特定条件时,才会创建该 Bean。
示例:

 @ConditionalOnProperty(name = "cache.type", havingValue = "redis")
 @Bean
 public SimpleCacheService<K, V> redisTemplateService(RedisTemplate<K, V> redisTemplate) {
     return new RedisCacheService<>(redisTemplate);
 }

说明:只有当配置文件中 cache.type 属性的值为 redis 时,才会创建 redisTemplateService Bean。

@ConditionalOnExpression
作用:当 SpEL 表达式的结果为 true 时,才会创建该 Bean。
示例:

 @ConditionalOnExpression("${cache.enabled:true}")
 @Bean
 public SimpleCacheService<K, V> cacheService() {
     return new DefaultCacheService<>();
 }

说明:只有当配置文件中的 cache.enabled 属性为 true 或未设置时,才会创建 cacheService Bean。

@ConditionalOnWebApplication@ConditionalOnNotWebApplication
作用:分别在 Web 应用程序和非 Web 应用程序中生效。
示例:

 @ConditionalOnWebApplication
 @Bean
 public WebService webService() {
     return new DefaultWebService();
 }
 
 @ConditionalOnNotWebApplication
 @Bean
 public NonWebService nonWebService() {
     return new DefaultNonWebService();
 }

这些注解可以帮助开发者根据不同的运行环境和配置条件来动态装配 Bean,提高应用的灵活性和可扩展性。

相关文章:

  • 做期货看那个网站比较专业中小企业网络营销现状
  • 免费网站软件免费下载安装seo静态页源码
  • 网站建设 广州网站建设专业公司如何网上免费做推广
  • 个人做网站排版百度搜索引擎优化的养成良好心态
  • 虚拟主机 多个网站免费的短视频app大全
  • 自己有一个域名怎么做网站徐州网站设计
  • 网络原理-TCP/IP
  • rviz可视化(一、可视化点云)
  • 4. Flink SQL访问HiveCatalog
  • <em>凤</em><em>凰</em><em>购</em><em>彩</em><em>大</em><em>厅</em>
  • 三个核心文件:src\App.vue文件,index.html文件,src\main.js文件 的关系与运行流程解析(通俗形象)
  • L2-037 包装机 (分数25)(详解)
  • DeepSeek协助优化-GTX750Ti文物显卡0.65秒卷完400MB float 音频512阶时域FIR
  • OTN(Optical Transport Network)详解
  • RK3588,V4l2 读取Gmsl相机, Rga yuv422转换rgb (dma), 实现零拷贝
  • 【Deep Reinforcement Learning Hands-On Third Edition】【序】
  • Python Django基于人脸识别的票务管理系统(附源码,文档说明)
  • 运算放大器(三)运算放大器的典型应用
  • Zoomlt使用
  • 解决:在运行 plt.show()`时,程序会等待你手动关闭图片窗口才能继续往下执行
  • 基于YOLO11/WVP的电梯系统开发-模型训练与rk3568移植
  • HarmonyOS NEXT——【鸿蒙实现录音识别(语音转文字)】
  • Node.js 技术原理分析系列 —— Node.js 调试能力分析
  • 正则化是什么?
  • Anolis OS 8.4修复CVE-1999-0554漏洞记录
  • 操作系统——线程的概念和特点