读取yml配置文件以及工具类静态方法获取配置文件配置信息

获取阿里文件上传配置信息为例

yml配置文件配置好

  oss:  
    access-key-id: xxxxx
    accesskey-secret: xxxxxx
    bucket-name: xxxx
    endpoint: https://oss-cn-shenzhen.aliyuncs.com

使用配置类读取配置文件信息

/**
 * @ClassName: AliOssProperties
 * @Description: oss配置文件类
 * @author: ruyi
 */
@Getter
@Setter
@ConfigurationProperties("oss")
public class AliOssProperties {

    private String endpoint;

    private String accessKeyId;

    private String accesskeySecret;

    private String bucketName;

}

在配置类(AliOssProperties )加入静态字段

   private static String endpointSerialize;

    private static String accessKeyIdSerialize;

    private static String accesskeySecretSerialize;
 
    private static String publicBucketName;

AliOssProperties 加入静态方法并且使用@PostConstruct项目启动时加载配置信息

    @PostConstruct
    public void setOssProperties() {
        endpointSerialize = this.endpoint;
        accessKeyIdSerialize = this.accessKeyId;
        accesskeySecretSerialize = this.accessKeySecret;
        publicBucketName=this.bucketName;
    }

    public static String getEndpointSerialize(){
        return endpointSerialize;
    }
    public static String getAccessKeyIdSerialize(){
        return accessKeyIdSerialize;
    }
    public static String getAccesskeySecretSerialize(){
        return accesskeySecretSerialize;
    }
    public static String getPublicBucketName(){
        return publicBucketName;
    }

在工具类静态方法获取获取配置类的参数

/**
 * 获取可访问可下载工具类
 *
 * @author ruyi
 * @Data: 2022/2/28
 * @version v1.0
 **/
public class AccessibleUrlUtils {
  
    /**
     *
     *
     * @param url 数据库url
     * @return 可访问的url
     */
    public static String handleOssUrl(String url) {
     System.out.println( AliOssProperties.getEndpointSerialize()+AliOssProperties.getAccessKeyIdSerialize()+AliOssProperties.getAccesskeySecretSerialize());
    }
}

你可能感兴趣的:(java,java,后端,开发语言)