一、@ConditionalOnProperty简介

在开发基于SpringBoot的项目时,经常看到下面的条件注解:

  • ConditionalOnProperty
  • ConditionalOnResource
  • ConditionalOnBean
  • ConditionalOnClass
  • ConditionalOnMissingBean
  • ConditionalOnMissingClass

其中@ConditionalOnProperty是最为常见的一种,尤其用在自动配置类中,下面看下它的源码:

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


	//name的别名,和value不可同时使用
	String[] value() default {};

	/**配置项的前缀,例如完整的配置是config.person.enable=true
	* 那 prefix=“config.person”
	*/
	String prefix() default "";

	/**
	配置项的属性,例如完整的配置是config.person.enable=true
	在前面已经设置 prefix=“config.person”
	那么 name=“enable”;
	
	如果prefix没有设置,那么name可以是整个配置项,例如下面:
	name=“config.person.enable”,效果和上面的一样
	 */
	String[] name() default {};

	/**
	 * 会将配置文件中的值和havingValue的值对比,如果一样则加载Bean,例如:
	 * 
	 * config.person.enable=true,havingValue=“true”,加载Bean
	 * 
	 * config.person.enable=false,havingValue=“false”,加载Bean
	 * 
	 * config.person.enable=ok,havingValue=“ok”,加载Bean
	 * 
	 * config.person.enable=false,havingValue=“true”,不加载Bean
	 * 
	 * config.person.enable=ok,havingValue=“no”,不加载Bean
	 * 
	 * 当然havingValue也可以不设置,只要配置项的值不是false或“false”,都加载Bean,例如:
	 * 
	 * config.person.enable=ok,不设置havingValue,加载Bean
	 * 
	 * config.person.enable=false,不设置havingValue,不加载Bean
	 */
	String havingValue() default "";

	/**
	 * 如果在配置文件中没有该配置项,是否加载Bean
	 */
	boolean matchIfMissing() default false;

}

二、详细代码示例

1. 配置文件中配置

config:
  person:
    enable: true

2. 指定prefix+name

指定prefix+name,配置项的值不是false或“false”,加载Bean

@Configuration
public class MyBeanConfig {

    @Bean
    @ConditionalOnProperty(prefix = "config.person", name = "enable")
    public Person person1(){
        return new Person("Bill Gates", 66);
    }
}

3. 只指定name 或者 value

只指定name 或者 value,要写完整配置项路径,配置项的值不是false或“false”,加载Bean

@Configuration
public class MyBeanConfig {

    @Bean
    @ConditionalOnProperty(value = "config.person.enable")
    public Person person1(){
        return new Person("Bill Gates", 66);
    }
}

4. 指定了havingValue

指定了havingValue,要把配置项的值与havingValue对比,一致则加载Bean

@Bean
@ConditionalOnProperty(prefix = "config.person", name = "enable", havingValue = "true")
public Person person1(){
   return new Person("Bill Gates", 66);
}
  • config.person.enable=true,havingValue=“true”,加载Bean

  • config.person.enable=false,havingValue=“false”,加载Bean

  • config.person.enable=ok,havingValue=“ok”,加载Bean

  • config.person.enable=false,havingValue=“true”,不加载Bean

  • config.person.enable=ok,havingValue=“no”,不加载Bean

  • 当然havingValue也可以不设置,只要配置项的值不是false或“false”,都加载Bean

  • config.person.enable=ok,不设置havingValue,加载Bean

  • config.person.enable=false,不设置havingValue,不加载Bean

@Configuration
public class MyBeanConfig {

    @Bean
    @ConditionalOnProperty(prefix = "config.person", name = "ifenable")
    public Person person1(){
        return new Person("Bill Gates", 66);
    }
}

上面代码配置项是true,但不设置havingValue,加载Bean

5. 配置项是false,havingValue = false"

配置项是false,havingValue = false",两者相等,加载Bean

config:
  person:
    enable: false
@Bean
@ConditionalOnProperty(prefix = "config.person", name = "enable", havingValue = "false")
public Person person1(){
    return new Person("Bill Gates", 66);
}

6. 配置项是false,不设置havingValue

配置项是false,不设置havingValue,不加载Bean

@Bean
@ConditionalOnProperty(prefix = "config.person", name = "enable")
public Person person1(){
    return new Person("Bill Gates", 66);
}

7. 缺少配置,但配置了matchIfMissing = true

配置文件缺少配置,但配置了matchIfMissing = true,加载Bean,否则不加载。

@Configuration
public class MyBeanConfig {

    @Bean
    @ConditionalOnProperty(prefix = "config.person", name = "enable", 
    havingValue = "true", matchIfMissing = true)
    public Person person1(){
        return new Person("Bill Gates", 66);
    }
}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐