java类静态属性通过@Value注解获取.yml 配置值

  • spring boot yml配置文件
server:
  port: 8888
  tomcat:
    uri-encoding: UTF-8
disk:
  shiro:
    excludeUrls: /account/account/createAdmin,/account/role/saveRole,/account/premission/savePremission
  oss:
    endpoint: oss
    access_key_id: LTA
    access_key_secret: R1eR
    bucket_name: cloud
    domain_name: my
  • 代码获取配置值
package org.disk.modules.oss;

import cn.hutool.core.bean.BeanUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * oss object init
 */
@Configuration
public class OssBoot {

    private static String ENDPOINT;

    private static String ACCESS_KEY_ID;

    private static String ACCESS_KEY_SECRET;

    public static String BUCKET_NAME;

    public static String DOMAIN_NAME;

    @Value("${disk.oss.endpoint}")
    public void setENDPOINT(String ENDPOINT) {
        OssBoot.ENDPOINT = ENDPOINT;
    }
    @Value("${disk.oss.access_key_id}")
    public void setAccessKeyId(String accessKeyId) {
        OssBoot.ACCESS_KEY_ID = accessKeyId;
    }
    @Value("${disk.oss.access_key_secret}")
    public void setAccessKeySecret(String accessKeySecret) {
        OssBoot.ACCESS_KEY_SECRET = accessKeySecret;
    }
    @Value("${disk.oss.bucket_name}")
    public void setBucketName(String bucketName) {
        OssBoot.BUCKET_NAME = bucketName;
    }
    @Value("${disk.oss.domain_name}")
    public void setDomainName(String domainName) {
        OssBoot.DOMAIN_NAME = domainName;
    }
}

 

你可能感兴趣的:(java,spring,boot,java)