• 配置文件读取(程序员的成长之路---第2篇)


    开篇后第一个知识点,本想写数据库连接池,仔细一想还是放弃了,改写为《配置文件读取》。

    毕竟项目中的基础信息,比如说数据库连接信息是需要配置在项目的配置文件中的。

    重点介绍

    ResourceBundle.getBundle("配置文件名称").getString("配置文件内的key值");

    配置文件名称:config.properties

    #数据库连接文件配置。普通项目的数据库连接应该就一个文件,但此处有两个。为了解决数据库集群时负载均衡或者数据库的读写分离准备。
    jdbcs=SqlServer,jdbc53
    #文件上传服务器的配置,普通项目应该用不到 filesRealPath
    =Z: filesContextPath=http://127.0.0.1:8080/files/

    #缓存服务器配置(项目进行分布式部署时,解决缓存同步。与memcached、redis功能类似) cacheServer=http://127.0.0.1:8080/um/CacheService #缓存服务器配置(不同项目之间共享缓存时使用) cacheProject=um
    #服务端发送的api接口地址 remoteAnswer
    =http://127.0.0.1:8080/um/remoteAnswer

    普通小项目,很多配置项用不到。
    但作为架构师,这些是必备要素,要不然怎么在别人面前显示自己的高大上呢!

     java类内容:

    package com.keji10.core.commons;
    
    import java.util.ResourceBundle;
    
    import org.apache.log4j.Logger;
    
    import com.keji10.core.exception.ManagerException;
    
    public class ConfigBean {
        // 单例begin
        private Logger logger = Logger.getLogger(this.getClass());
        private static ConfigBean configBean = new ConfigBean();
        private ResourceBundle resourceBundle;
    
        private ConfigBean() {
            resourceBundle = ResourceBundle.getBundle("config");
        }
    
        public static ConfigBean getInstance() {
            return configBean;
        }
    
        // 单例end
    
        /**
         * 获取数据库的配置文件名
         * 
         * @return
         */
        public String getJdbcs() {
            try {
                return resourceBundle.getString("jdbcs");
            } catch (Exception e) {
                return "jdbc";
            }
        }
    
        /**
         * 获取文件保存磁盘的根目录地址
         * 
         * @return
         */
        public String getFilesRealPath() {
            try {
                return resourceBundle.getString("filesRealPath");
            } catch (Exception e) {
                return null;
            }
        }
    
        /**
         * 获取文件保存磁盘后的访问根地址
         * 
         * @return
         */
        public String getFilesContextPath() {
            try {
                return resourceBundle.getString("filesContextPath");
            } catch (Exception e) {
                return null;
            }
        }
    
        /**
         * 获取缓存服务的地址
         * 
         * @return
         */
        public String getCacheServer() {
            try {
                return resourceBundle.getString("cacheServer");
            } catch (Exception e) {
                return null;
            }
        }
    
        /**
         * 获取当前项目在缓存服务器的别名
         * 
         * @return
         */
        public String getCacheProject() {
            try {
                return resourceBundle.getString("cacheProject");
            } catch (Exception e) {
                return null;
            }
        }
    
        /**
         * 获取远程服务器接口地址
         * 
         * @return
         */
        public String getRemoteAnswer() {
            try {
                return resourceBundle.getString("remoteAnswer");
            } catch (Exception e) {
                return null;
            }
        }
    
        /**
         * 其他自定义配置
         * 
         * @param type
         * @return
         * @throws ManagerException
         */
        public String getType(String type) throws ManagerException {
            String value;
            try {
                value = resourceBundle.getString(type);
            } catch (Exception e) {
                value = null;
            }
            if (Validate.isNull(value)) {
                logger.error("获取配置文件" + type + "失败");
                throw new ManagerException("获取配置文件" + type + "失败");
            }
            return value;
        }
    
    }
    下一篇介绍,数据库连接池的封装。
    很多人会问,Spring的数据库连接是现成的,我们为什么还要自己封装数据库连接池呢?
    那我就要反问了,你能使Spring的数据库连接池很轻松的支持项目的
    负载均衡和读写分离吗?
    架构师的成长之路是艰难的,坚持!
  • 相关阅读:
    pika的阻塞式使用
    使用 Nuget安装DLL
    StackExchange.Redis的使用
    MongoDB 学习笔记(9)--- Limit与Skip方法
    MongoDB 学习笔记(8)---$type 操作符
    MongoDB学习笔记(7)--- 条件操作符
    MongoDB学习笔记(6)--find
    阿里巴巴Java开发规范手册
    python获取当前文件路径
    断网环境下利用pip安装Python离线安装包
  • 原文地址:https://www.cnblogs.com/ltlm/p/10194395.html
Copyright © 2020-2023  润新知