1
votes

starting to use Couchbase Java SDK with spring boot, to access the cluster, bucket and collection (sdk 3.+) I created a @Configuration class as follows:

@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;
}

Thing is that I was required to use data from application.properties file. I tried autowiring Environment to get properties without luck, and multiple ways of using @Value that also gives me errors. Is there a way to configure my connection to the DB by getting the values from the file and having the bean cluster that I can autowire? Or is maybe my solution completely wrong?

Here's how tried creating the bean Cluster using @Value:

@Bean
public Cluster cluster(@Value("${host}" String host,@Value("${user}" String user,
@Value("${pass}" String pass) {
    return Cluster.connect(host,user,pass);
}
2

2 Answers

1
votes

I changed the class to the following and got it working, I am not sure if it's the right solution though:

@Autowired
private Environment env;

@Bean
public Collection collection() {
    return bucket().defaultCollection();
}

@Bean
public Bucket bucket() {
    return cluster().bucket(env.getProperty("storage.bucket"));
}

@Bean
public Cluster cluster() {
    return Cluster.connect(env.getProperty("storage.host"),
            env.getProperty("storage.username"), env.getProperty("storage.password"));
}
0
votes

Firstly you should create couchbaseProperties class.

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "couchbase")
@Getter
@Setter
public class CouchbaseProperties {
    private String host;
    private String userName;
    private String password;
    private String bucketName;
}

And then you should give parameters in application.properties file.

couchbase.host=localhost
couchbase.userName=Administrator
couchbase.password=password
couchbase.bucketName=blackbox

Finally you can use data in Database class.

    @Configuration
    public class Database {
    
        private final CouchbaseProperties couchbaseProperties;
    
        public Database(CouchbaseProperties couchbaseProperties) {
            this.couchbaseProperties = couchbaseProperties;
        }
        @Bean
        public Cluster cluster() {
            return Cluster.connect(couchbaseProperties.getHost(), ClusterOptions
                    .clusterOptions(couchbaseProperties.getUserName(), 
                     couchbaseProperties.getPassword()));
        }
   }