Why doesn't the @ConditionalOnBean(RedisTemplate.class) work?

Viewed 60

I have a FlowControlConfig dependent on RedisTemplate. But @ConditionalOnBean(RedisTemplate.class) doesn't work, and FlowControlCache isn't an instance. I debugged my code and confirmed that RedisTemplate already been instance. This is my code

// RedisTemplate instance
@Bean
@SuppressWarnings("all")
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, Object> template = new RedisTemplate<>();
    template.setConnectionFactory(factory);
    Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
    StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
    template.setKeySerializer(stringRedisSerializer);
    template.setHashKeySerializer(stringRedisSerializer);
    template.setValueSerializer(jackson2JsonRedisSerializer);
    template.setHashValueSerializer(jackson2JsonRedisSerializer);
    template.afterPropertiesSet();
    return template;
}
@Configuration
@ConditionalOnBean(RedisTemplate.class)
public class FlowControlConfig {

    @Bean
    public FlowControlCache flowControlCache(RedisTemplate redisTemplate) {
        return new FlowControlCache() {
            @Override
            public void save(String key, Integer value, Long time, TimeUnit timeUnit) {
                redisTemplate.opsForValue().set(key, value, time, timeUnit);
            }

            @Override
            public void save(String key, Integer value) {
                redisTemplate.opsForValue().set(key, value);
            }

            @Override
            public Integer get(String key) {
                return (Integer) redisTemplate.opsForValue().get(key);
            }
        };
    }
}

But when I change @ConditionalOnBean to @ConditionalOnMissingBean, it works, and RedisTemplate is not null, like this:

@Configuration
@ConditionalOnMissingBean(RedisTemplate.class)
public class FlowControlConfig {

    @Bean
    public FlowControlCache flowControlCache(RedisTemplate redisTemplate) {
        return new FlowControlCache() {
            @Override
            public void save(String key, Integer value, Long time, TimeUnit timeUnit) {
                redisTemplate.opsForValue().set(key, value, time, timeUnit);
            }

            @Override
            public void save(String key, Integer value) {
                redisTemplate.opsForValue().set(key, value);
            }

            @Override
            public Integer get(String key) {
                return (Integer) redisTemplate.opsForValue().get(key);
            }
        };
    }
}

Why is it that, it seems like @ConditionalOnMissingBean and @ConditionalOnBean are contrary?

1 Answers

because FlowControlConfig condition annotation evaluate when scan your app base package,in that time,@Bean method (redisTemplate) 's beanDefintion does load not yet

Related