英文:
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");
}
}
在设置环境变量之前:
在设置环境变量之后:
英文:
>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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论