How to access spring.application.instance_id programmatically?

11.2k views Asked by At

I have the following content in my "applicationname.yml" file of my Spring Boot/Cloud application. How can i get the value of spring.application.instance_id in my java code ? This "applicationname.yml" file is hosted in the 'Spring Cloud Config Server'.

eureka:
  password: password
  client:
    registryFetchIntervalSeconds: 5
    serviceUrl:
      defaultZone: ${vcap.services.${PREFIX:}eureka.credentials.uri:http://user:password@localhost:8761}/eureka/
  instance:
    preferIpAddress: true
    leaseRenewalIntervalInSeconds: 10
    metadataMap:
      instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${server.port}}}

I have a java class where i am trying to access this variable's value using the @Value annotation and it gives me an error. Here is what i have to get and print the value in the java class

@Value("${eureka.instance.instanceId}")
private String EinstanceId;

@Value("${spring.application.instance_id}")
private String SinstanceId;

Error Message:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.citigroup.ccp.psg.error.PSGErrorFilter.instanceId; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'eureka.instance.instanceId' in string value "${eureka.instance.instanceId}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 49 more
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'eureka.instance.instanceId' in string value "${eureka.instance.instanceId}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:175)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:801)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:955)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 51 more
2

There are 2 answers

4
hi_my_name_is On BEST ANSWER

You miss metadata block. Use:

@Value("${eureka.instance.metadataMap.instanceId}") String instanceId;

As far as I know there is no instance_id property in spring.application namespace.

3
spencergibb On

Use the @Value annotation with Spring Expression Language

import org.springframework.beans.factory.annotation.Value;

//...

    @Value("${spring.application.instance_id}")
    private String instanceId;

//...