Azure environment variable not working: "Factory method 'dataSource' threw exception with message: URL must start with 'jdbc' "

huangapple go评论81阅读模式
英文:

Azure environment variable not working: "Factory method 'dataSource' threw exception with message: URL must start with 'jdbc' "

问题

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.

英文:

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

得分: 1

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

要访问代码中的环境变量,请在App Service->Settings->Configuration中创建一个变量,并将其值设置如下:

下面是我的示例Controller类的代码片段,用于获取指定的环境变量:

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");
    }
}

在设置环境变量之前:

Azure environment variable not working: "Factory method 'dataSource' threw exception with message: URL must start with 'jdbc' "

在设置环境变量之后:

Azure environment variable not working: "Factory method 'dataSource' threw exception with message: URL must start with 'jdbc' "

英文:

>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:

Azure environment variable not working: "Factory method 'dataSource' threw exception with message: URL must start with 'jdbc' "

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:

Azure environment variable not working: "Factory method 'dataSource' threw exception with message: URL must start with 'jdbc' "

After Setting the Environment Variable:

Azure environment variable not working: "Factory method 'dataSource' threw exception with message: URL must start with 'jdbc' "

huangapple
  • 本文由 发表于 2023年5月14日 01:57:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定