源码及参数说明
package org.springframework.boot.autoconfigure.condition;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.env.Environment;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {
// 数组,获取对应property名称的值,与name不可同时使用
String[] value() default {};
// 配置属性名称的前缀,比如spring.http.encoding.enabled
String prefix() default "";
// 数组,配置属性完整名称或部分名称
// 可与prefix组合使用,组成完整的配置属性名称,与value不可同时使用
String[] name() default {};
// 可与name组合使用,比较获取到的属性值与havingValue给定的值是否相同,相同才加载配置
String havingValue() default "";
// 缺少该配置属性时是否可以加载。如果为true,没有该配置属性时也会正常加载;反之则不会生效
boolean matchIfMissing() default false;
}
示例
比如用在自定义主从数据库切换,从库是否可用
@Bean(name = "slaveDataSource")
@ConfigurationProperties(DataSourceProperties.SLAVE_PREFIX)
@ConditionalOnProperty(prefix = DataSourceProperties.SLAVE_PREFIX, name = "enabled", havingValue = "true")
public DataSource slaveDataSource() {
SingleDataSourceProperty slave = dataSourceProperties.slave;
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setName(DataSourceConstants.SLAVE);
druidDataSource.setDriverClassName(slave.getDriverClassName());
druidDataSource.setUrl(slave.getUrl());
druidDataSource.setUsername(slave.getUserName());
druidDataSource.setPassword(slave.getPassword());
return druidDataSource;
}