怎样在 Spring Boot 中界说和读取 自界说设置

[复制链接]
发表于 2026-1-11 02:27:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
媒介

在Spring Boot中界说和读取自界说设置是一样平常开辟中常见的需求,它允许我们以机动的方式管理应用的设置信息,无论是通过外部设置文件(如application.properties或application.yml)还是通过环境变量。
作为高级步伐员,我们必要把握这一技能,以确保应用的可设置性和可维护性。以下是一个具体的步调阐明,包罗示例代码,展示怎样在Spring Boot中界说和读取自界说设置。
在Spring Boot中,你可以通过以下步调界说和读取自界说设置:

  •         在application.properties或application.yml中界说自界说设置项。
比方,在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. }
复制代码
确保@ConfigurationProperties注解的prefix属性与你在设置文件中界说的前缀相匹配。Spring Boot会主动将设置属性绑定到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中,这通常通过@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. }
复制代码
留意,@ConfigurationProperties(prefix = "myapp")注解指定了设置属性的前缀为myapp,如许Spring Boot就能主动将application.properties中全部以myapp开头的属性绑定到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中界说了自界说设置属性,并通过@ConfigurationProperties注解将它们绑定到了JavaBean上。末了,我们通过依赖注入的方式在应用中读取这些设置。这种方式不但进步了代码的可读性和可维护性,还使得设置管理变得更加机动和方便。在现实开辟中,公道使用Spring Boot的设置管理功能,可以大大提升应用的机动性和可扩展性。
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表