前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot读取yml或者properties配置数据

springboot读取yml或者properties配置数据

原创
作者头像
伍六七AI编程
修改2020-12-11 18:06:17
1.9K0
修改2020-12-11 18:06:17
举报
文章被收录于专栏:preparedprepared

1 使用@Value注解

一般用于 非static

@Value 注解即可获取。

增加注解@RefreshScope,可以使得配置实时生效(实践使用nacos做配置中心的时候)

@Configuration
@RefreshScope
public class InquiryConfig {

    @Value("${prepared.template.filepath}")
    private String templateFilePath;
}    

static也可以使用@Value,使用setter方法.

    public static String tempFileName;

    private static String templateFilePath;

    @Value("${esign.templateFile}")
    public void setTxtResource(String templateFilePath) {
        tempFileName = templateFilePath;
    }

2使用Environment

可以获取static修饰的变量

@Autowired
private Environment env;

public static String hbase_zookeeper_quorum;

public static String hbase_zookeeper_property_clientPort;

@PostConstruct
private void init() {
    hbase_zookeeper_quorum = env.getProperty("hbase.quorum");
    hbase_zookeeper_property_clientPort = env.getProperty("hbase.clientPort");
    conf = HBaseConfiguration.create();
    System.out.println(hbase_zookeeper_quorum);
    System.out.println(".>>>>>>>>>>>>>>>>>");
    conf.set("hbase.zookeeper.quorum", hbase_zookeeper_quorum);
    conf.set("hbase.zookeeper.property.clientPort", hbase_zookeeper_property_clientPort);
    try {
        conn = ConnectionFactory.createConnection(conf);
        admin = conn.getAdmin();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
  1. 增加 Environment
  2. PostConstuct 注解方法,第一次执行
  3. env.getProperty("hbase.quorum") 获取具体值

3 读取文件的方式

读取config.preperties文件的所有配置

使用方式:

SysConfig.getInstance().getProperty("属性key");
// 比如
SysConfig.getInstance().getProperty("message.templateid");

代码

package com.prepared.config;

import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Properties;

public class SysConfig {
    private Properties props = null;// config.properties
    private static volatile SysConfig conf;

    private SysConfig() {
        props = new Properties();
        loadConfigProps();
    }

    public static SysConfig getInstance() {
        if (conf == null) {
            synchronized (SysConfig.class) {
                if (conf == null) {
                    conf = new SysConfig();
                }
            }
        }
        return conf;
    }

    public void loadConfigProps() {
        InputStream is = null;
        try {
            is = getClass().getResourceAsStream("/WEB-INF/classes/config.properties");
            if (is == null) {
                is = getClass().getResourceAsStream("/config.properties");
            }
            InputStreamReader reader = new InputStreamReader(is, "UTF-8");
            props.load(reader);
            Iterator<String> iter = props.stringPropertyNames().iterator();
            while (iter.hasNext()) {
                String key = iter.next();
                props.setProperty(key, props.getProperty(key));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                    is = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public String getProperty(String key) {
        String tmp = props.getProperty(key);
        if (StringUtils.isNotEmpty(tmp)) {
            return tmp.trim();
        }
        return tmp;
    }

    public String getProperty(String key, String defaultValue) {
        String tmp = props.getProperty(key, defaultValue);
        if (StringUtils.isNotEmpty(tmp)) {
            return tmp.trim();
        }
        return tmp;
    }

    public int getPropertyInt(String key) {
        String tmp = props.getProperty(key);
        if (StringUtils.isNotEmpty(tmp)) {
            return Integer.parseInt(tmp.trim());
        }
        return 0;

    }

    public int getPropertyInt(String key, int defaultValue) {
        String tmp = props.getProperty(key);
        if (StringUtils.isNotEmpty(tmp)) {
            return Integer.parseInt(tmp.trim());
        }
        return defaultValue;
    }

    public long getPropertyLong(String key, long defaultValue) {
        String tmp = props.getProperty(key);
        if (StringUtils.isNotEmpty(tmp)) {
            return Integer.parseInt(tmp.trim());
        }
        return defaultValue;
    }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 使用@Value注解
    • 2使用Environment
      • 3 读取文件的方式
      相关产品与服务
      微服务引擎 TSE
      微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档