使用@Value注解获取yml配置文件中的自定义属性(三种不同环境下详细获取方式:变量+静态变量+静态代码块)


一。配置文件说明

1.application.yml配置:(这里使用的是application-local.yml配置文件)

在这里插入图片描述

2.application-local.yml自定义属性配置:

在这里插入图片描述

二。变量属性获取

  @Value("${fastdfs_config.path}")
  private String path;

三。静态变量获取

1.第一种方式:

    //fastdfs配置文件所在路径
    private static String filePath;

    @Value("${fastdfs_config.path}")
    public void setFilePath(String path){
        this.filePath=path;
    }

2.第二种方式:

    @Value("${fastdfs_config.path}")
    private static String filePath;
    
    @PostConstruct
    public void setPath(String path){
        this.filePath=path;
    }

四。静态代码块获取


static {
        try {
            //获取配置文件中自定义的fastdfs_config.path值
            String filePath=(String)getCommonYml("fastdfs_config.path");
            ClientGlobal.init(filePath);
        } catch (IOException e) {
            logger.error("Get FastDFS config file fail!",e);
        } catch (MyException e) {
            logger.error("FastDF init fail!",e);
        }
    }

    //获取application-local.yml文件中的属性配置
    public static Object getCommonYml(Object key){
        Resource resource = new ClassPathResource("/application-local.yml");
        Properties properties = null;
        try {
            YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
            yamlFactory.setResources(resource);
            properties =  yamlFactory.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return properties.get(key);
    }

特别注意的是:需要与@Component、@Service等Spring注解配合开启!!!

额外方式:使用两个配置类加载静态代码块

读取配置文件配置类UserFileConfig:

package com.hdyanfa.interact.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @Description:读取配置文件配置类
 * @Author :zks
 * @Date :15:40 2021/3/4
 */
@Configuration
public class UserFileConfig {

    /**
     * fastDFS配置所在地址
     */
    @Value("${fastdfs_config.path}")
    private String fastDfs_path;

    public UserFileConfig() {
    }

    public String getFastDfs_path() {
        return fastDfs_path;
    }

    public void setFastDfs_path(String fastDfs_path) {
        this.fastDfs_path = fastDfs_path;
    }

    @PostConstruct
    public void initFileConfig() {
        FastDfsConfig.initFileConfigUitls(this);
    }

}

fastdfs配置类FastDfsConfig:

package com.hdyanfa.interact.config;

/**
 * @Description:fastdfs配置类
 * @Author :zks
 * @Date :15:52 2021/3/4
 */
public class FastDfsConfig {
    /**
     * fastDFS配置文件所在地址
     */
    private static String fastDfs_path;

    public FastDfsConfig() {
    }

    public static String getFastDfs_path() {
        return fastDfs_path;
    }

    public static void setFastDfs_path(String fastDfs_path) {
        FastDfsConfig.fastDfs_path = fastDfs_path;
    }

    public static void initFileConfigUitls(UserFileConfig userFileConfig) {
        FastDfsConfig.fastDfs_path=userFileConfig.getFastDfs_path();

    }

}

使用:

在这里插入图片描述

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Keson Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值