Springboot基础使用@Conditional多条件注册Bean

95 篇文章 3 订阅
60 篇文章 2 订阅

环境:springboot2.3.10


需求:有这么一个Bean它被注册的条件是需要满足多个条件下才能被注册。如下

pack:
  datasource:
    enabled: true
---
pack:
  cache:
	  enabled: true

只有在这两个属性都为true时才注册Bean。

方法1 @ConditionalOnExpression

注解说明:

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnExpressionCondition.class)
public @interface ConditionalOnExpression {

  /**
   * The SpEL expression to evaluate. Expression should return {@code true} if the
   * condition passes or {@code false} if it fails.
   * @return the SpEL expression
   */
  String value() default "true";

}

表达式SpEL表达式 要么返回true,要么返回false。

使用内置的@ConditionalOnExpression表达式条件注解。

@Bean
@ConditionalOnExpression("${pack.datasource.enabled:true} and ${pack.cache.enabled:true}")
public Animal animal() {
  return new Animal() ;
}
pack:
  datasource:
    enabled: true
---    
pack:
  cache:
    enabled: false

只有这两个属性都配置为true时Animal Bean才会被注册。

方式2 AllNestedConditions & AnyNestedCondition

使用AllNestedConditions所有条件都满足的情况下才会注册Bean

public class DataSourceAllCondition extends AllNestedConditions {
	
  public DataSourceAllCondition() {
    super(ConfigurationPhase.REGISTER_BEAN) ;
  }
  public DataSourceAllCondition(ConfigurationPhase configurationPhase) {
    super(configurationPhase);
  }
  @ConditionalOnBean(UsersController.class)
  static class ExistClass {
  }
  @ConditionalOnProperty(prefix = "pack.datasource", name = "enabled", havingValue = "true", matchIfMissing = false)
  static class PropertyEnabled {
  }
  @ConditionalOnProperty(prefix = "pack.cache", name = "enabled", havingValue = "true", matchIfMissing = false)
  static class PropertyEnabled {
  }
}
@Bean
@Conditional(DataSourceAnyCondition.class)
public Animal animal() {
  return new Animal() ;
}

这里只有3个条件都满足了才会注册。当前上下文中存在UsersController Bean,pack.datasource.enabled=true, pack.cache.enabled=true 3个条件同时成立才会被注册。

AnyNestedCondition 只要有一个条件成立就会注册Bean。

public class DataSourceAnyCondition extends AnyNestedCondition {
	public DataSourceAnyCondition() {
		super(ConfigurationPhase.REGISTER_BEAN) ;
	}
	public DataSourceAnyCondition(ConfigurationPhase configurationPhase) {
		super(configurationPhase);
	}
	@ConditionalOnProperty(prefix = "pack.datasource", name = "enabled", havingValue = "true", matchIfMissing = false)
	static class PropertyEnabled {
	}
  @ConditionalOnProperty(prefix = "pack.cache", name = "enabled", havingValue = "true", matchIfMissing = false)
  static class PropertyEnabled {
  }
}

两个条件只要有一个成立即可。

完毕!!!

关注一波呗

wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

Spring Cloud Sentinel 基础配置

Spring Cloud Sentinel 流控限流

Spring Cloud Sentinel 熔断降级

Spring Cloud Gateway应用详解1之谓词

Spring Cloud Nacos 开启权限验证

SpringCloud Nacos 整合feign

SpringCloud Alibaba 之 Nacos 服务

Spring Cloud Sentinel整合Feign

SpringCloud Nacos 服务提供者

SpringCloud Nacos 服务消费者

Springboot中Redis事务的使用及Lua脚本

spring data jpa 高级应用

Spring Boot Security防重登录及在线总数

SpringCloud Zookeeper配置中心详解

Spring MVC 异步请求方式

Spring Security记住我功能实现及源码分析

Spring Retry重试框架的应用

使用Spring Boot Admin实时监控你的系统

Springboot基础使用@Conditional多条件注册Bean

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以使用@Conditional注解来根据条件创建bean。@Conditional注解可以在bean的创建过程中,根据条件决定是否创建bean。这使得我们可以根据应用程序的配置或环境来决定是否创建bean。例如,我们可以使用@Conditional注解来创建一个bean,只有当特定的系统属性或环境变量被设置时才会创建它。 下面是一个使用@Conditional注解的例子: ```java @Configuration public class AppConfig { @Bean @Conditional(PropertyExistsCondition.class) public MyBean myBean() { return new MyBean(); } } ``` 在上面的例子中,我们使用PropertyExistsCondition类来检查一个系统属性是否存在。只有当这个属性存在时,才会创建MyBean bean。 另一个常用的注解是@AutoConfigureAfter。这个注解用于指定一个bean在另一个特定的bean之后自动配置。这个注解主要用于自动配置的场景,其中一个bean需要依赖于另一个bean。下面是一个使用@AutoConfigureAfter注解的例子: ```java @Configuration @AutoConfigureAfter(DatabaseConfig.class) public class AppConfig { // ... } ``` 在上面的例子中,我们指定了AppConfig bean应该在DatabaseConfig bean之后自动配置。这将确保在配置AppConfig bean之前,DatabaseConfig bean已经被正确地配置了。 总之,@Conditional和@AutoConfigureAfter是两个非常有用的注解,可以帮助我们在Spring Boot应用程序中根据条件创建bean,并指定bean之间的依赖关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值