springboot使用配置类从 application.yml 或 application.properties 文件中读取静态属性

springboot使用配置类从 application.yml 或 application.properties 文件中读取静态属性

1. 配置类定义
通过 @ConfigurationProperties(prefix = “data-base-check”),Spring Boot 将带有 data-base-check 前缀的属性从 application.yml 或 application.properties 文件中读取,并自动赋值给 DatabaseCheckConfig 中对应的属性。

2. 属性绑定
DatabaseCheckConfig 中定义了多个配置项属性,比如 verifyServiceUrl、attachmentImagePath 等,这些属性将通过配置文件绑定的方式注入具体值。例如:

# application.yml 中的配置示例
data-base-check:
  verifyServiceUrl:
    - http://localhost:8080/api/verify
    - http://localhost:8081/api/verify
  attachmentImagePath: /path/to/attachment/images
  attachmentImageRequestUrl: http://external.com/api/image-check
  rootPath: /data/root
  fileTransferServerPort: 8085
  fileTransferServerPorts: "8085,8086,8087"
  arcgisDataServiceUrl: http://arcgis.com/api/data
  sqliteEncryptBinPath: /path/to/sqlite/encrypt
  dataCheckTool: /path/to/data/check/tool
  maxCheckThreadNum: 10

3. 使用 @Autowired 注入 DatabaseCheckConfig

当需要在业务逻辑中使用该配置类时,可以通过 @Autowired 注入 DatabaseCheckConfig,从而访问这些配置属性。例如:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DataService {
    
    

    private final DatabaseCheckConfig databaseCheckConfig;

    @Autowired
    public DataService(DatabaseCheckConfig databaseCheckConfig) {
    
    
        this.databaseCheckConfig = databaseCheckConfig;
    }

    public void checkData() {
    
    
        // 使用配置属性
        List<String> urls = databaseCheckConfig.getVerifyServiceUrl();
        String attachmentPath = databaseCheckConfig.getAttachmentImagePath();
        // 进行业务逻辑操作
    }
}

一旦 Spring Boot 启动,DatabaseCheckConfig 会自动将配置文件中对应的 data-base-check 下的配置项赋值到类的属性中,供业务逻辑使用,无需额外操作即可完成绑定。

具体项目中示例:
在这里插入图片描述
DatabaseCheckConfig:

package com.geofly.dataservicecenter.api.common.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;


@Data
@Component
@ConfigurationProperties(prefix = "data-base-check")
public class DatabaseCheckConfig {
    
    
    /**
     * 数据验证服务地址
     */
    private List<String> verifyServiceUrl;

    /**
     * 请求的客户端id,不需要了
     */
    @Deprecated
    private String requestClientId;

    /**
     * 数据库关联的图片路径
     */
    private String attachmentImagePath;

    /**
     * 附件请求的影像路径,用来给第三方工具检查图像信息
     */
    private String attachmentImageRequestUrl;


    /**
     * 资源目录根路径
     */
    private String rootPath;

    /**
     * 文件传输端口
     */
    private int fileTransferServerPort;



    /**
     * 文件传输端口
     */
    private String fileTransferServerPorts;

    /**
     * arcgis数据服务请求地址
     */
    private String arcgisDataServiceUrl;

    /**
     * sqlite加密工具路径
     */
    private String sqliteEncryptBinPath;

    /**
     * 数据库检查工具路径
     */
    private String dataCheckTool;

    private Integer maxCheckThreadNum;


}

yml:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_61950936/article/details/143387270