Reputation: 21
I have a Springboot app that is trying to connect and read values from an app config resource.
Some keys in the Azure app config (which I am unable to change) are in this format
`
/application/config.datasource.jdbc-url
/application/config.datasource.password
/application/config.datasource.username`
I have a config Java class with prefix ("config"), but I don't know what member variables I should have in order to access "datasource.jdbc-url", "datasource.password" etc.
If the app config was just /application/config.username
then I could just use the below in my Java class
String username;
but for some of the configs that include multiple dots and some dashes (which Java identifiers can't have), how can I read the values?
Thank you!
Upvotes: 0
Views: 987
Reputation: 493
You have two options depending on what you are looking for. You can either use @Value("config.datasource.jdbc-url")
or you can use nested properties.
public class Datasource {
private String username;
private String password;
private String jdbcurl;
...
}
@ConfigurationProperties(prefix = "config")
class MyProperties {
private Datasource datasource = new Datasource();
...
}
Upvotes: 1
Reputation: 3694
Adding to @Moarchy Chan@ConfigurationProperties(prefix = "config.datasource")
In Azure portal
>App configuration
> Configuration explorer
, I have created keys and values for Username, Password and Jdbc url.
This is my Class UserProperties,
@ConfigurationProperties(prefix = "config.datasource")
public class UserProperties {
private String username;
private String password;
private String jdbcurl;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getJdbcurl() {
return jdbcurl;
}
public void setJdbcurl(String jdbcurl) {
this.jdbcurl = jdbcurl;
}
}
Here is the UserController I have created,
public class UserController {
private final UserProperties properties;
public UserController(UserProperties properties) {
this.properties = properties;
}
@GetMapping("/Anu")
public String getUsername() {
return "username: " + properties.getUsername();
}
@GetMapping("/vyshu")
public String getPassword() {
return "password: " + properties.getPassword();
}
@GetMapping("/priya")
public String getJdbcurl() {
return "jdbcurl: " + properties.getJdbcurl();
}
}
This the bootstrap for my code, you should add your connection string in {},
spring.cloud.azure.appconfiguration.stores[0].connection-string= ${APP_CONFIGURATION_CONNECTION_STRING
}
This is the output for my Username,
This is the output for my Password,
This is the output for my jdbcurl,
Upvotes: 0
Reputation: 31
Your use case is similar to this sample azure-spring-cloud-starter-appconfiguration-config-sample, please try to define the properties class to bind the configurations in Azure App Configuration.
Upvotes: 0