springboot 实现apication.yml中配置动态参数实现静态获取(static)

2021年6月25日 14点热度 0条评论 来源: SuperChen12356

已连接阿里云oss为例

aliyun:
  oss:
    # API鉴权
    accessKeyId: # 访问身份验证中用到用户标识
    accessKeySecret: # 用户用于加密签名字符串和oss用来验证签名字符串的密钥
    # oss访问域名(内网)
    endpoint: 
    bucketName:  # oss的存储空间
    policyExpire: 300 # url有效期(S)
    maxSize: 500 # 上传文件大小(M)
import lombok.Data;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/** * 阿里云OSS配置 */
@Data
@Configuration
@ConfigurationProperties(prefix = "aliyun.oss")
public class AliYunOssConfig implements InitializingBean { 

    /** * 阿里云 oss 站点 */
    private String endpoint;

    /** * 阿里云 oss 公钥 */
    private String accessKeyId;

    /** * 阿里云 oss 私钥 */
    private String accessKeySecret;

    /** * 阿里云 oss 文件根目录 */
    private String bucketName;

    /** * url有效期(S) */
    private Long policyExpire;

    /** * 上传文件大小(M) */
    private Long maxSize;

    // 方便直接获取
    public static String JAVA_END_POINT;
    public static String JAVA_ACCESS_KEY_ID;
    public static String JAVA_ACCESS_KEY_SECRET;
    public static String JAVA_BUCKET_NAME;
    public static Long JAVA_POLICY_EXPIRE;
    public static Long JAVA_MAX_SIZE;

    @Override
    public void afterPropertiesSet() { 
        JAVA_END_POINT = endpoint;
        JAVA_ACCESS_KEY_ID = accessKeyId;
        JAVA_ACCESS_KEY_SECRET = accessKeySecret;
        JAVA_BUCKET_NAME = bucketName;
        JAVA_POLICY_EXPIRE = policyExpire;
        JAVA_MAX_SIZE = maxSize;
    }

}


 <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
    原文作者:SuperChen12356
    原文地址: https://blog.csdn.net/SuperChen12356/article/details/118226189
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。