简体   繁体   English

无法使用@ Configuration.properties读取application.yml

[英]Can't read application.yml using @Configuration.properties

Here's my application.yml file: 这是我的application.yml文件:

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. 我正在尝试将该文件读入我的Spring Boot应用程序。 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: 这是我的CouchbaseContext.java文件:

import lombok.Getter; 导入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 . 我在尝试访问CouchbaseConfig.javacouchbaseContext对象时得到了NPE Can someone help me with what I am doing wrong? 有人可以帮我解决我做错的事情吗?

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

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

moreover you can remove @Configuration in CouchbaseContext 此外,您可以在CouchbaseContext中删除@Configuration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用application.yml配置SpringBoot - SpringBoot Configuration with application.yml 如何在springboot中从application.yml中读取带有特殊字符的属性 - How to read properties with special characters from application.yml in springboot Docker 图像无法读取 Spring 引导应用程序上的 application.yml 文件 - Docker image can't read application.yml file on Spring Boot Application 向JHipster Cloud application.yml添加属性会导致注册表配置失败 - Adding properties to JHipster Cloud application.yml causes registry configuration to fail 无法读取application.yml文件 - application.yml file cannot be read 如何从 spring-boot 应用程序中的 application.yml 文件中读取属性 - How to read properties from application.yml file in spring-boot application Spring 应用程序的库模块如何向应用程序的“application.yml”添加额外的配置? - How can a library module of a Spring application add additional configuration to the application's `application.yml`? Spring Boot-无法读取Yaml属性文件application.yml - spring boot- unable to read yaml properties file application.yml 使用 application.yml/properties 的批处理侦听器的 Spring kafka 集成属性 - Spring kafka integration properties for batch listener using application.yml/properties 使用外部 tomcat 时如何覆盖 application.yml 中的属性? - How to override properties from application.yml when using external tomcat?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM