0


如何在 Spring Boot 中定义和读取 自定义配置

前言

在Spring Boot中定义和读取自定义配置是日常开发中常见的需求,它允许我们以灵活的方式管理应用的配置信息,无论是通过外部配置文件(如

  1. application.properties

  1. application.yml

)还是通过环境变量。

作为高级程序员,我们需要掌握这一技能,以确保应用的可配置性和可维护性。以下是一个详细的步骤说明,包括示例代码,展示如何在Spring Boot中定义和读取自定义配置。

在Spring Boot中,你可以通过以下步骤定义和读取自定义配置:

  • application.propertiesapplication.yml中定义自定义配置项。

例如,在

  1. application.yml

中添加:

  1. app:
  2. custom:
  3. my-property: value
  • 创建一个配置类来绑定这些属性。
  1. import org.springframework.boot.context.properties.ConfigurationProperties;
  2. import org.springframework.context.annotation.Configuration;
  3. @Configuration
  4. @ConfigurationProperties(prefix = "app.custom")
  5. public class CustomProperties {
  6. private String myProperty;
  7. public String getMyProperty() {
  8. return myProperty;
  9. }
  10. public void setMyProperty(String myProperty) {
  11. this.myProperty = myProperty;
  12. }
  13. }
  • 在Spring Bean中注入CustomProperties类来使用配置值。
  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. public class MyComponent {
  5. private final CustomProperties customProperties;
  6. @Autowired
  7. public MyComponent(CustomProperties customProperties) {
  8. this.customProperties = customProperties;
  9. }
  10. public void printCustomProperty() {
  11. System.out.println(customProperties.getMyProperty());
  12. }
  13. }

确保

  1. @ConfigurationProperties

注解的

  1. prefix

属性与你在配置文件中定义的前缀相匹配。Spring Boot会自动将配置属性绑定到

  1. CustomProperties

类的字段上。然后你可以在应用程序的任何部分通过自动装配的方式来使用这些配置值。

或者:

定义自定义配置属性

  1. # application.properties
  2. myapp.name=Spring Boot Application
  3. myapp.description=This is a demo application for custom configuration
  4. myapp.server.port=8081

创建配置类

接下来,我们需要创建一个配置类来绑定这些自定义属性。在Spring Boot中,这通常通过

  1. @ConfigurationProperties

注解实现,它允许我们将配置文件中的属性绑定到JavaBean上。

  1. import org.springframework.boot.context.properties.ConfigurationProperties;
  2. import org.springframework.context.annotation.Configuration;
  3. @Configuration
  4. @ConfigurationProperties(prefix = "myapp")
  5. public class MyAppConfig {
  6. private String name;
  7. private String description;
  8. private int serverPort;
  9. // 标准的getter和setter方法
  10. public String getName() {
  11. return name;
  12. }
  13. public void setName(String name) {
  14. this.name = name;
  15. }
  16. public String getDescription() {
  17. return description;
  18. }
  19. public void setDescription(String description) {
  20. this.description = description;
  21. }
  22. public int getServerPort() {
  23. return serverPort;
  24. }
  25. public void setServerPort(int serverPort) {
  26. this.serverPort = serverPort;
  27. }
  28. }

注意,

  1. @ConfigurationProperties(prefix = "myapp")

注解指定了配置属性的前缀为

  1. myapp

,这样Spring Boot就能自动将

  1. application.properties

中所有以

  1. myapp

开头的属性绑定到

  1. MyAppConfig

类的相应字段上。

读取配置

我们可以在应用的任何地方通过依赖注入的方式读取这些配置。例如,在一个Controller中

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class MyAppConfigController {
  6. private final MyAppConfig myAppConfig;
  7. @Autowired
  8. public MyAppConfigController(MyAppConfig myAppConfig) {
  9. this.myAppConfig = myAppConfig;
  10. }
  11. @GetMapping("/config")
  12. public String getConfig() {
  13. return "Name: " + myAppConfig.getName() + ", Description: " + myAppConfig.getDescription() + ", Server Port: " + myAppConfig.getServerPort();
  14. }
  15. }

总结

通过上述步骤,我们成功地在Spring Boot中定义了自定义配置属性,并通过

  1. @ConfigurationProperties

注解将它们绑定到了JavaBean上。最后,我们通过依赖注入的方式在应用中读取这些配置。这种方式不仅提高了代码的可读性和可维护性,还使得配置管理变得更加灵活和方便。在实际开发中,合理利用Spring Boot的配置管理功能,可以大大提升应用的灵活性和可扩展性。


本文转载自: https://blog.csdn.net/A_aspectJ/article/details/142906564
版权归原作者 A_aspectJ项目开发 所有, 如有侵权,请联系我们删除。

“如何在 Spring Boot 中定义和读取 自定义配置”的评论:

还没有评论