0

I tried setting up environment variables on application.properties with the ${VARIABLE_NAME} and then in the azure configuration I set up the key value pair. But it doesn't seem to work. I also tried running System.getenv before the application starts, but that didn't do much. So I keep getting the error that the URL must start with jdbc since its not using the env variable I set.

The line below is from the application.properties:

spring.datasource.url=${DB_CONNECTION_STRING}

and in azure i set an application setting in the configuration section for DB_CONNECTION_STRING with the value of jdbc.....

Then I even tried to set it as a connection string in the azure config as well with the 'SQLAZURECONNSTR' prefix, and still nothing works.

Then I even tried this before main: String dbConnectionString = System.getenv("DB_CONNECTION_STRING");

Not sure if that does anything.

1 Answer 1

1

I also tried running System.getenv before the application starts, but that didn't do much.

I have tried to reproduce with System.getenv() method in my environment, it worked as expected and could achieve the expected results.

To access the env variables in your code, create a variable in App Service->Settings-> Configuration and assign its value as shown below:

Below is the code snippet of my sample Controller class to fetch the specified env variable:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public  class  AzureAppController  {
    @GetMapping(value  =  "/my-var")
    public  String  getMyVar()  {
        return  "The value of MY_VAR is: "  +  System.getenv("MY_VAR");
    }
}

Response:

Before Setting the Environment Variable:

After Setting the Environment Variable:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.