3

I am working on application which is deployed on AWS. I have set database configuration (URL, UserName, Password) in AWS Environment properties.

AWS environment Variables

Now how can I access these variables in my spring boot application?

My application.properties file looks like:

spring.datasource.driver-class-name = <DRIVER>
spring.datasource.url = <URL>
spring.datasource.username = <USERNAME>
spring.datasource.password = <PASSWORD>

Note: Currently I am accessing database details from application.properties file

Yogesh
  • 79
  • 2
  • 9

1 Answers1

9

To use environment variables in spring boot application.properties you can use the usual Spring placeholder notation:

spring.datasource.url = ${JDBC_CONNECTION:default_value_connection}

Further explanation: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-placeholders-in-properties

You can set JDBC_CONNECTION value in AWS Elastic beanstalk. If JDBC_CONNECTION environment variable not set it will use the 'default_value_connection'.

Abhilekh Singh
  • 2,845
  • 2
  • 18
  • 24
  • 1
    I just tested this out to make sure: if your datasource url is somehing like 'jdbc:mysql://endpoint/db', this still works. {JDBC_CONNECTION:jdbc:mysql://endpoint/db} – Jordan Mackie Jul 03 '18 at 15:50
  • For everyone using the YAML file instead of the proprieties file: don't forget to use the double quotes spring: datasource: url: "${JDBC_CONNECTION:default_value_connection}" – Ahmed Aziz Oct 25 '22 at 20:07