一、獲取屬性:用(:設置默認值)redis
@Value("${mode.cache:redis}")spring
private String mode;bootstrap
二、當用@Value取屬性的時候,SpringBoot默認是報錯的。app
/**.net
* 描 述:忽略配置不存在server
* 建立時間: 2018/05/22rabbitmq
*/get
@Configurationkafka
public class PropertyIgnoreConfig {it
public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
PropertySourcesPlaceholderConfigurer c = new PropertySourcesPlaceholderConfigurer();
c.setIgnoreUnresolvablePlaceholders(true);
return c;
}
}
三、根據屬性動態加載類
當application中的key/value,相等的時候加載對應的類。若是屬性不存在,不會加載對應類
@Component
@ConditionalOnProperty(name = RedisMode.CAHCE_MODE_KEY,havingValue = RedisMode.CACHE_MODE_EHCACHE)
public class RedisHealthIndicator implements HealthIndicator {}
當application中的key存在spring.kafka.bootstrap-servers爲true,不存在爲false
@ConditionalOnProperty(prefix = "spring.kafka",name = "bootstrap-servers")
public class RedisHealthIndicator implements HealthIndicator {}
設置默認值爲true,當spring.rabbitmq.addresses不存在的時候,加載對應類
@ConditionalOnExpression("'${spring.rabbitmq.addresses:true}'=='true'")
public class RabbitHealthIndicator implements HealthIndicator {