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

相关推荐
Magnum Lehar1 小时前
3d游戏引擎EngineTest的系统实现3
java·开发语言·游戏引擎
就叫飞六吧1 小时前
Spring Security 集成指南:避免 CORS 跨域问题
java·后端·spring
Mcworld8571 小时前
java集合
java·开发语言·windows
天黑请闭眼1 小时前
IDEA:程序编译报错:java: Compilation failed: internal java compiler error
java·intellij-idea
苍煜2 小时前
Maven构建流程详解:如何正确管理微服务间的依赖关系-当依赖的模块更新后,我应该如何重新构建主项目
java·微服务·maven
冼紫菜2 小时前
[特殊字符]CentOS 7.6 安装 JDK 11(适配国内服务器环境)
java·linux·服务器·后端·centos
isyangli_blog3 小时前
(1-4)Java Object类、Final、注解、设计模式、抽象类、接口、内部类
java·开发语言
秋野酱4 小时前
Spring Boot 项目的计算机专业论文参考文献
java·spring boot·后端
码视野4 小时前
基于Spring Boot和Vue的在线考试系统架构设计与实现(源码+论文+部署讲解等)
vue.js·spring boot·系统架构
士别三日&&当刮目相看4 小时前
数据结构*优先级队列(堆)
java·数据结构