Prashant Pandey
Prashant Pandey

Reputation: 4652

Can't read application.yml using @Configuration.properties

Here's my application.yml file:

server:
  port: 8089

schedulers:
  identity:
    origin: http://localhost:7189
    client-id: 72612d8e-b78e-446c-9397-354435696ec3
    secret: 173a7f6a-8263-11e7-bb31-be2e44b06b34
    username: integrationCLient
    password: P@ssword
    token-cache-backoff: 5
    default-token-cache-ttl: 5
    endpoints:
      validateToken: /v3/api/validateToken
      issueToken: /v3/api/oauth/v2/token
    httpClient:
      numOfRetries: 3
      backOffInMillis: 5
  couchbase:
    bootstrapHost: localhost
    bucket: default
    connectionTimeout: 1000

I am trying to read the file into my Spring Boot application. Here's the relevant code:

@Configuration
@ConfigurationProperties(prefix = "schedulers")
@Getter
@Setter
public class CouchbaseConfig {

  CouchbaseContext couchbaseContext = new CouchbaseContext();

  @Bean
  public Bucket bucket() {
    System.out.println("askfjaslkfjafa" + couchbaseContext.getBootstrapHost());    //throws NPE
    CouchbaseEnvironment env =
        DefaultCouchbaseEnvironment.builder()
            .connectTimeout(Long.parseLong(couchbaseContext.getConnectionTimeout()))
            .build();
    Cluster cluster = CouchbaseCluster.create(env, couchbaseContext.getBootstrapHost());
    return cluster.openBucket(couchbaseContext.getBucket());
  }
}

Here's my CouchbaseContext.java file:

import lombok.Getter; import lombok.Setter;

@Getter
@Setter
public class CouchbaseContext {
    private String bootstrapHost;
    private String bucket;
    private String connectionTimeout;
}

I am getting NPE while trying to access the couchbaseContext object in CouchbaseConfig.java. Can someone help me with what I am doing wrong?

Upvotes: 0

Views: 613

Answers (1)

Pianov
Pianov

Reputation: 1663

@EnableConfigurationProperties({
    CouchbaseContext.class
})
@Configuration
public class CouchbaseConfig {

    @Bean
    public Bucket bucket(CouchbaseContext couchbaseContext) { ... }
}

moreover you can remove @Configuration in CouchbaseContext

Upvotes: 2

Related Questions