0

I have created a WebserviceCredentials class for config as below and used @autowired for WebserviceCredentials in a @component class it does not work (shows null values) , but when used in @restcontroller class it works, appreciate your help

@Component
@ConfigurationProperties(prefix="webservice")
public class WebserviceCredentials {

    @Value("${webservice.EndPoint}")
    private String webserviceEndpoint;
    @Value("${webservice.Username}")
    private String username;
    @Value("${webservice.Password}")
    private String password;

    public String getwebserviceEndpoint() {
        return webserviceEndpoint;
    }
    public void setwebserviceEndpoint(String webserviceEndpoint) {
        this.webserviceEndpoint = webserviceEndpoint;
    }

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }


}

2 Answers2

0

The setup isn't inline with how to use ConfigurationProperties

@ConfigurationProperties(prefix="webservice")
public class WebserviceCredentials {
private String endpoint;
private String username;
private String password;

public String getEndpoint() {
    return endpoint;
}
public void setEndpoint(String endpoint) {
    this.endpoint = endpoint;
}

public String getUsername() {
    return username;
}
public void setUsername(String username) {
    this.username = username;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
}

Then add @EnableConfigurationProperties(WebserviceCredentials.class) to a Configuration or the Main Application Class.

See https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties for further information

Darren Forsythe
  • 10,712
  • 4
  • 43
  • 54
0

The @ConfigurationProperties(prefix="webservice") should reside on your POJO that represents your properties file. To use it in your WebserviceCredentials class, you can use

public class WebserviceCredentials {
  @Autowire 
  private ConfigurationProperties configurationProperties;
  //the rest of your code
}

You can refer to this link I just recently posted

Mapping YMAL properties

Steven
  • 59
  • 2