[英]Why is the @ConditionalOnBean does't work?
I have a FlowControlConfig dependent on RedisTemplate.我有一个依赖于 RedisTemplate 的 FlowControlConfig。 But @ConditionalOnBean(RedisTemplate.class) does't work and FlowControlCache does't been instance.但是 @ConditionalOnBean(RedisTemplate.class) 不起作用并且 FlowControlCache 不是实例。 I debug my code and confirmed that RedisTemplate already been instance.我调试了我的代码并确认 RedisTemplate 已经是实例。 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 work, and RedisTemplate is not null, like this:但是当我将@ConditionalOnBean 更改为@ConditionalOnMissingBean 时,它起作用了,并且RedisTemplate 不是null,像这样:
@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 that, it seems like @ConditionalOnMissingBean and @ConditionalOnBean are contrary?为什么会这样,@ConditionalOnMissingBean 和 @ConditionalOnBean 似乎相反?
because FlowControlConfig condition annotation evaluate when scan your app base package,in that time,@Bean method (redisTemplate) 's beanDefintion does load not yet因为 FlowControlConfig 条件注释在扫描您的应用程序库 package 时进行评估,在那个时候,@Bean 方法 (redisTemplate) 的 beanDefintion 尚未加载
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.