1:ConditionalOnProperty 为本class或者bean方法被初始化的条件
@ConditionalOnProperty(prefix = “petlove.cache”, name = "type", havingValue = "REDIS", matchIfMissing = true)
@EnableConfigurationProperties({RedisProperties.class, CustomCacheProperties.class})
@RequiredArgsConstructor
@Slf4j
public class RedisAutoConfigure {
}
表示当yaml配置如下时,该配置RedisAutoConfigure才会被注入到容器
2: EnableConfigurationProperties:使使用 @ConfigurationProperties 注解的类生效
@ConfigurationProperties(
prefix = "spring.redis"
)
public class RedisProperties {
private int database = 0;
private String url;
private String host = "localhost";
private String username;
private String password;
private int port = 6379;
}
yaml中的配置如下:
同RedisProperties中的部分变量一一对应