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,提高应用的灵活性和可扩展性。

相关推荐
快来卷java2 分钟前
MySQL篇(一):慢查询定位及索引、B树相关知识详解
java·数据结构·b树·mysql·adb
凸头36 分钟前
I/O多路复用 + Reactor和Proactor + 一致性哈希
java·哈希算法
头孢头孢1 小时前
k8s常用总结
运维·后端·k8s
TheITSea1 小时前
后端开发 SpringBoot 工程模板
spring boot·后端
Asthenia04121 小时前
编译原理中的词法分析器:从文本到符号的桥梁
后端
慵懒学者1 小时前
15 网络编程:三要素(IP地址、端口、协议)、UDP通信实现和TCP通信实现 (黑马Java视频笔记)
java·网络·笔记·tcp/ip·udp
anda01091 小时前
11-leveldb compact原理和性能优化
java·开发语言·性能优化
Asthenia04121 小时前
用RocketMQ和MyBatis实现下单-减库存-扣钱的事务一致性
后端
Pasregret2 小时前
04-深入解析 Spring 事务管理原理及源码
java·数据库·后端·spring·oracle
Micro麦可乐2 小时前
最新Spring Security实战教程(七)方法级安全控制@PreAuthorize注解的灵活运用
java·spring boot·后端·spring·intellij-idea·spring security