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

相关推荐
m0_57046641几秒前
代码随想录算法训练营第二十八天 | 买卖股票的最佳实际、跳跃游戏、K次取反后最大化的数组和
java·开发语言·算法
豌豆花下猫39 分钟前
Python 潮流周刊#118:Python 异步为何不够流行?(摘要)
后端·python·ai
ST.J1 小时前
swing笔记
java·笔记
菩提树下的凡夫1 小时前
瑞芯微RV1126目标识别算法Yolov8的部署应用
java·算法·yolo
尚学教辅学习资料1 小时前
Ruoyi-vue-plus-5.x第五篇Spring框架核心技术:5.1 Spring Boot自动配置
vue.js·spring boot·spring
爱隐身的官人1 小时前
新后端漏洞(上)- Java RMI Registry反序列化漏洞
java·反序列化漏洞
叫我阿柒啊1 小时前
从Java全栈到前端框架:一次真实的面试对话与技术解析
java·javascript·typescript·vue·springboot·react·前端开发
晚安里1 小时前
Spring 框架(IoC、AOP、Spring Boot) 的必会知识点汇总
java·spring boot·spring
秋难降1 小时前
SQL 索引突然 “罢工”?快来看看为什么
数据库·后端·sql
爱隐身的官人2 小时前
新后端漏洞(上)- Aapache Tomcat AJP 文件包含漏洞(CVE-2020-1938)
java·tomcat·ajp