Reputation: 1414
I deployed spring boot service to AWS and application.properties has refers to one of the OS environment variable which is being set as part of deployment process. It seems like Spring Boot fails to resolve the OS environment variable when booting up.
If I echo the variable it seems to be set correctly -
ubuntu@ip-10-227-74-206:~$ echo $EC2_INSTANCE_ID
i-de8c136d
application.properties file refers to that variable as below -
service.hostId=aws-${EC2_INSTANCE_ID}
I see below error when booting up the application -
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.foo.services.registration.config; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'EC2_INSTANCE_ID' in string value "${EC2_INSTANCE_ID}"
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
Any idea what I could be doing wrong? Do I need to add any prefix like "sys:" when referring in application.properties file?
Upvotes: 2
Views: 8172
Reputation: 9
Use @ConfigurationProperties
and @Value
annotation to injected OS var in your beans.
Ref: Spring Boot documentation about Externalized Configuration
Upvotes: 0
Reputation: 1414
After spending lot of time, it feels like something to do with init script in spring-boot. I found some workaround it not ideal but got things done -
Source environment variable in the init conf file.
source /etc/environment
This makes environment variable able to parent process which starts spring boot application. Now pass all environment variable as JAVA_OPTS in conf file
JAVA_OPTS="-Denv.variable=${var.foo}"
Upvotes: 4