【发布时间】:2020-06-25 17:33:01
【问题描述】:
开始使用带有spring boot的Couchbase Java SDK,访问集群、存储桶和集合(sdk 3.+)我创建了一个@Configuration类,如下:
@Configuration
public class Database {
Cluster cluster = Cluster.connect("localhost", "Administrator", "password");
Bucket bucket = cluster.bucket("blackbox");
@Bean
public Collection collection() {
return this.bucket.defaultCollection();
}
@Bean
public Bucket bucket() {
return this.bucket;
}
@Bean
public Cluster cluster() {
return this.cluster;
}
问题是我需要使用 application.properties 文件中的数据。我尝试了自动装配环境来获取属性而没有运气,以及使用@Value 的多种方式,这也给了我错误。有没有办法通过从文件中获取值并拥有我可以自动装配的 bean 集群来配置我与数据库的连接?还是我的解决方案完全错误?
这是尝试使用@Value 创建 bean 集群的方法:
@Bean
public Cluster cluster(@Value("${host}" String host,@Value("${user}" String user,
@Value("${pass}" String pass) {
return Cluster.connect(host,user,pass);
}
【问题讨论】:
标签: java spring-boot couchbase spring-data-couchbase