@ConditionalOnProperty的用法
一、说明
SpringBoot中有时候需要控制配置类是否生效,使用 @ConditionalOnProperty 注解来控制@Bean是否生效。
二、例子
在配置类头部添加@Configuration和@ConditionalOnProperty注解
- 例子1
@Configuration
@ConditionalOnProperty(prefix = "config",name = "enable",havingValue = "true", matchIfMissing = true)
public class CorssConfig {
@Bean
public CorsFilter corsFilter(){
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.setAllowCredentials(false);
corsConfiguration.addAllowedMethod("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addExposedHeader(HttpHeaders.ACCEPT);
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**",corsConfiguration);
return new CorsFilter(source);
}
- 例子2
@Configuration
@ConditionalOnProperty(value= {"config.enable"}, havingValue = "true", matchIfMissing = true)
public class CorssConfig {
@Bean
public CorsFilter corsFilter(){
//内容同上
}
三、讲解
- 源码
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {
/**
* Alias for {@link #name()}.
* @return the names
*/
String[] value() default {};
/**
* A prefix that should be applied to each property. The prefix automatically ends
* with a dot if not specified. A valid prefix is defined by one or more words
* separated with dots (e.g. {@code "acme.system.feature"}).
* @return the prefix
*/
String prefix() default "";
/**
* The name of the properties to test. If a prefix has been defined, it is applied to
* compute the full key of each property. For instance if the prefix is
* {@code app.config} and one value is {@code my-value}, the full key would be
* {@code app.config.my-value}
* <p>
* Use the dashed notation to specify each property, that is all lower case with a "-"
* to separate words (e.g. {@code my-long-property}).
* @return the names
*/
String[] name() default {};
/**
* The string representation of the expected value for the properties. If not
* specified, the property must <strong>not</strong> be equal to {@code false}.
* @return the expected value
*/
String havingValue() default "";
/**
* Specify if the condition should match if the property is not set. Defaults to
* {@code false}.
* @return if should match if the property is missing
*/
boolean matchIfMissing() default false;
}
- 含义
源码里这个注解是有5个参数的,首先了解其含义
- value
String数组的配置类别名
- prefix
String类型的配置类前缀
- name
String数组的配置名称,要结合prefix来用
- havingValue
String类型的默认值,与配置的值对比值,当两个值相同返回true,配置类生效
- matchIfMissing
boolean类型的缺省值,缺少配置时,是否可以加载;缺少配置时,true:正常加载,false:报错
- 使用
在配置文件中,以SpringBoot为例,在application.yml中,配置config.enable
config:
enable:ok
在配置类上可如下配置:
@ConditionalOnProperty(prefix = "config",name = "enable",havingValue = "ok", matchIfMissing = true)
或者
@ConditionalOnProperty(value= {"config.enable"}, havingValue = "ok", matchIfMissing = true)
四、总结
@ConditionalOnProperty可以很方便去控制不同环境下对配置的要求,方便调试,省去麻烦