I have problem with configuration of application properties in springboot

60 views Asked by At

I'm a developper and I start with SpringBoot to make an application. I follow a course in udemy. I have problem when I try to run application.This is error description in console :


APPLICATION FAILED TO START


Description:

CurrencyConfigurationService is annotated with @ConstructorBinding but it is defined as a regular bean which caused dependency injection to fail.

Action:

Update your configuration so that CurrencyConfigurationService is defined via @ConfigurationPropertiesScan or @EnableConfigurationProperties.

Process finished with exit code 0

and this is the content of my Class CurrencyConfigurationService

package spring.boot.learnspringboot.services;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "currency-service")
@Component
public class CurrencyConfigurationService {
    private String url;
    private String username;
    private String key;

    public CurrencyConfigurationService(String url, String username, String key) {
        this.url = url;
        this.username = username;
        this.key = key;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}

Someone can help me to debug this problem?

I try to make an application using SpringBoot, it's the start with springboot for me and I have a problem that I am no idea to solve it

1

There are 1 answers

0
Andrei Lisa On

To fix it you have to do some changes:

CurrencyConfigurationService:

@ConfigurationProperties(prefix = "currency-service")
public class CurrencyConfigurationService {
  private String url;
  private String username;
  private String key;

 
  public CurrencyConfigurationService(String url, String username, String key) {
    this.url = url;
    this.username = username;
    this.key = key;
  }

  public String getUrl() {
    return url;
  }

  public void setUrl(String url) {
    this.url = url;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getKey() {
    return key;
  }

  public void setKey(String key) {
    this.key = key;
  }
}

Main class:

@SpringBootApplication
@ConfigurationPropertiesScan
public class ProductApisApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProductApisApplication.class, args);
    }

}

And I think your application.yml file are as following one:

currency-service:
  url: url
  username: username
  key: key

Or if it is application.properties file

currency-service.url=url
currency-service.username=username
currency-service.key=key