Aanchal Arora
Aanchal Arora

Reputation: 11

Unable to read property from application.yml using @Value in Spring boot

My code is unable to read a property value from application.yml in 1 class whereas it is able to read in another class. Please see my class below:

@Component
@EnableConfigurationProperties
public class DCCIAccessTokenProvider extends ClientCredentialsAccessTokenProvider{

   @Value("${authCode}")
   private String authorizationCode="";

   @Override
   protected OAuth2AccessToken retrieveToken(AccessTokenRequest request, OAuth2ProtectedResourceDetails resource,
        MultiValueMap<String, String> form, HttpHeaders headers) throws OAuth2AccessDeniedException {
       headers.add("Authorization", "Basic "+authorizationCode);
       return super.retrieveToken(request, resource, form, headers);
   }
  }

Here, in authorizationCode, I am getting blank while debugging.

I have another class, in which value is getting read properly. Please see below:

  @Component
  @EnableConfigurationProperties
  public class SearchBookingProcessor {

  @Autowired
  private OAuth2RestTemplate dcciRestTemplate;

  public OAuth2RestTemplate getDcciRestTemplate() {
    return dcciRestTemplate;
  }

  public void setDcciRestTemplate(OAuth2RestTemplate dcciRestTemplate) {
    this.dcciRestTemplate = dcciRestTemplate;
  }

  @Value("${api.dcci.searchBooking}")
  private String DCCI_API="";

  @Value("${jipcc}")
  private String JIPCC="";

}

Here the values are getting read properly.

Please let me know what can be the issue here. Thanks in advance!!

Upvotes: 0

Views: 822

Answers (2)

Aanchal Arora
Aanchal Arora

Reputation: 11

Thanks for all the help. I resolved the problem with the help of my colleague. I was using new object of DCCIAccessTokenProvider, instead of getting it from ApplicationContext and hence it was not able to read the value.

Upvotes: 0

Prem Kumar Easwaran
Prem Kumar Easwaran

Reputation: 232

Is there any specific reason for assigning an empty string to the property variable? If that is to set the default value if the property is not available in the file, please try the below line.

@Value("${authCode:}")
   private String authorizationCode;

Note the ":" after authCode

Let me know how it goes.

Upvotes: 0

Related Questions