如何解决spring如何连接Redis和Couchbase
如何将 redis 和 couchbase 连接到我的 spring 应用程序。
我收到此错误 Parameter 0 of method couchbaseMappingContext in org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration required a single bean,but 2 were found: - couchbaseCustomConversions: defined by method 'customConversions' in class path resource [{classPath}/chat/config/CouchbaseConfiguration.class] - redisCustomConversions: defined in null
我只需要redis'看'一个包,其他的只需要连接couchbase。
Redis 配置
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
MappingJackson2HttpMessageConverter converter =
new MappingJackson2HttpMessageConverter(mapper);
return converter;
}
@Bean
public RedisTemplate<Long,?> redisTemplate(RedisConnectionFactory connectionFactory) {
RedisTemplate<Long,?> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
// Add some specific configuration here. Key serializers,etc.
return template;
}
Couchbase 配置
@EnableCouchbaseRepositories
@Configuration
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
@Value("${spring.data.couchbase.bucket-name}")
private String bucketName;
@Value("${spring.couchbase.username}")
private String username;
@Value("${spring.couchbase.password}")
private String password;
@Value("${spring.couchbase.connection-string}")
private String connectionString;
@Override
public String getConnectionString() {
return this.connectionString;
}
@Override
public String getUserName() {
return this.username;
}
@Override
public String getPassword() {
return this.password;
}
@Override
public String getBucketName() {
return this.bucketName;
}
}
当我第一次在终端中启动我的应用程序时,有以下信息:Spring Data Redis - 无法安全识别存储库候选接口的存储分配
解决方法
为了解决使用 customConversions bean 时的歧义,我们可以告诉 couchbase 配置类如何创建 customConversions bean。将以下代码添加到扩展 AbstractCouchbaseConfiguration 的类应该可以解决问题
@Bean
public CustomConversions customConversions() {
return super.customConversions();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。