如何从Java SpringBoot应用程序中读取Azure应用配置值

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

How to read Azure app config values from Java SpringBoot app

问题

以下是翻译好的部分:

我有一个Springboot应用程序,尝试连接并读取应用配置资源中的值。

Azure应用配置中的一些键(我无法更改)采用以下格式:

/application/config.datasource.jdbc-url

/application/config.datasource.password

/application/config.datasource.username

我有一个配置的Java类,其前缀是 "config",但我不知道应该有哪些成员变量才能访问 "datasource.jdbc-url"、"datasource.password" 等等。

如果应用配置只是 /application/config.username

那么我可以在我的Java类中使用以下内容:

String username;

但是对于一些包含多个点和一些破折号的配置(Java标识符不能包含),我该如何读取这些值?

谢谢!

英文:

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!

答案1

得分: 1

根据您的需求,您有两个选择。您可以使用@Value("config.datasource.jdbc-url"),或者您可以使用嵌套属性。

public class Datasource {

  private String username;

  private String password;

  private String jdbcurl;

  ...
}

@ConfigurationProperties(prefix = "config")
class MyProperties {

  private Datasource datasource = new Datasource();

  ...

}
英文:

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();

  ...

}

</details>



# 答案2
**得分**: 0

您的用例类似于此示例[azure-spring-cloud-starter-appconfiguration-config-sample][1]请尝试定义[properties class][2]以绑定Azure App Configuration中的配置项

[1]: https://github.com/Azure-Samples/azure-spring-boot-samples/tree/main/appconfiguration/azure-spring-cloud-starter-appconfiguration-config/azure-spring-cloud-starter-appconfiguration-config-sample
[2]: https://github.com/Azure-Samples/azure-spring-boot-samples/blob/4ba43ef615e295c01fba0359402c696e7f412049/appconfiguration/azure-spring-cloud-starter-appconfiguration-config/azure-spring-cloud-starter-appconfiguration-config-sample/src/main/java/example/MessageProperties.java#L13

<details>
<summary>英文:</summary>

Your use case is similar to this sample [azure-spring-cloud-starter-appconfiguration-config-sample][1], please try to define the [properties class][2] to bind the configurations in Azure App Configuration.


  [1]: https://github.com/Azure-Samples/azure-spring-boot-samples/tree/main/appconfiguration/azure-spring-cloud-starter-appconfiguration-config/azure-spring-cloud-starter-appconfiguration-config-sample
  [2]: https://github.com/Azure-Samples/azure-spring-boot-samples/blob/4ba43ef615e295c01fba0359402c696e7f412049/appconfiguration/azure-spring-cloud-starter-appconfiguration-config/azure-spring-cloud-starter-appconfiguration-config-sample/src/main/java/example/MessageProperties.java#L13

</details>



# 答案3
**得分**: 0

`Azure portal` > `App configuration` > `Configuration explorer`我已经为 UsernamePassword 和 Jdbc url 创建了键和值

这是我的 UserProperties 类

```java
@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;
    }
}

这是我创建的 UserController 类,

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

这是我的代码的引导部分,你应该将你的连接字符串添加在 {} 中,

spring.cloud.azure.appconfiguration.stores[0].connection-string= ${APP_CONFIGURATION_CONNECTION_STRING}

这是我的 Username 的输出,
如何从Java SpringBoot应用程序中读取Azure应用配置值

这是我的 Password 的输出,
如何从Java SpringBoot应用程序中读取Azure应用配置值

这是我的 jdbcurl 的输出,
如何从Java SpringBoot应用程序中读取Azure应用配置值

英文:

Adding to @Moarchy Chan@ConfigurationProperties(prefix = &quot;config.datasource&quot;)

In Azure portal>App configuration> Configuration explorer, I have created keys and values for Username, Password and Jdbc url.

如何从Java SpringBoot应用程序中读取Azure应用配置值
This is my Class UserProperties,

 @ConfigurationProperties(prefix = &quot;config.datasource&quot;)
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(&quot;/Anu&quot;)
public String getUsername() {
return  &quot;username: &quot; + properties.getUsername();
}
@GetMapping(&quot;/vyshu&quot;)
public String getPassword() {
return  &quot;password: &quot; + properties.getPassword();
}
@GetMapping(&quot;/priya&quot;)
public String getJdbcurl() {
return  &quot;jdbcurl: &quot; + 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,
如何从Java SpringBoot应用程序中读取Azure应用配置值
This is the output for my Password,
如何从Java SpringBoot应用程序中读取Azure应用配置值
This is the output for my jdbcurl,
如何从Java SpringBoot应用程序中读取Azure应用配置值

huangapple
  • 本文由 发表于 2023年2月6日 06:38:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355983.html
匿名

发表评论

匿名网友

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

确定