Spring技巧

spring @ConditionalOnProperty按条件

2023-12-24  本文已影响0人  饱饱想要灵感

@ConditionalOnProperty 是 Spring Boot 的一个注解,它用于条件化地创建 Bean。当指定的属性(或者一组属性)满足给定的条件时,就会创建对应的 Bean。

例如,你可以使用 @ConditionalOnProperty 注解来指定仅当某个配置项存在时,才创建某个 Bean。注解的主要属性包括:

例如:

@Configuration
@ConditionalOnProperty(name = "my.property", havingValue = "true", matchIfMissing = true)
public class MyConfiguration {
  // ...
}

在这个例子中,只有当 my.property 配置项的值为 true 或者这个配置项不存在时,MyConfiguration 类才会被 Spring 实例化。

上一篇下一篇

猜你喜欢

热点阅读