gpt4 book ai didi

java - 即使依赖 Bean 存在,创建 Bean 时出错

转载 作者:搜寻专家 更新时间:2023-11-01 02:36:52 24 4
gpt4 key购买 nike

我的配置类有一些依赖 Bean

public class WebConfig{
@Bean
@Qualifier("geojedis")
public StringRedisTemplate geoJedisTemplate(
@Qualifier("geographyJedisConnectionFactory") final JedisConnectionFactory connectionFactory) {

// Create a RedisTemplate implementation which is basically of string
// data structure.
StringRedisTemplate redisTemplate = new StringRedisTemplate(connectionFactory);

return redisTemplate;
}

@Bean
@Qualifier("capacityStringRedisTemplate")
public StringRedisTemplate capacityStringRedisTemplate(
@Qualifier("capacityJedisConnectionFactory") final JedisConnectionFactory connectionFactory) {

// Create a RedisTemplate implementation which is basically of string
// data structure.
StringRedisTemplate redisTemplate = new StringRedisTemplate(connectionFactory);

return redisTemplate;
}

@Bean
public JedisConnectionFactory geographyJedisConnectionFactory() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
return connectionFactory;
}

@Bean
public JedisConnectionFactory capacityJedisConnectionFactory() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
return connectionFactory;
}
}

但我收到以下错误。当我检查配置时,一切都很好,我还定义了 Qualifier 来映射正确的依赖项。非常感谢任何帮助。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisTemplate' defined in class path resource [org/springframework/boot/autoconfigure/redis/RedisAutoConfiguration$RedisConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.data.redis.connection.RedisConnectionFactory]: : No qualifying bean of type [org.springframework.data.redis.connection.RedisConnectionFactory] is defined: expected single matching bean but found 2: geographyJedisConnectionFactory,capacityJedisConnectionFactory; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.data.redis.connection.RedisConnectionFactory] is defined: expected single matching bean but found 2: geographyJedisConnectionFactory,capacityJedisConnectionFactory

最佳答案

如果 Spring 上下文中没有默认的“redisTemplate”,RedisAutoConfiguration 中会创建一个 bean。

@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(
RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
return template;
}

这需要一个 RedisConnectionFactory,但找到了两个。

作为变通方法,您可以创建一个名为“redisTemplate”的虚拟 RedisTemplate 而不是使用它。

因为它通过 bean 名称检查,所以只要没有尝试@Autowire 它,下面的内容就足够了:

@Bean
public Object redisTemplate() {
return new Object();
}

关于java - 即使依赖 Bean 存在,创建 Bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47529691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com