zoukankan      html  css  js  c++  java
  • java读取配置文件方法以及工具类

    第一种方式 :

    java工具类读取配置文件工具类      

    只是案例代码  抓取异常以后的代码自己处理

    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class PropertyUtil {
        private static Properties props;
        static{
            loadProps();
        }
    
        synchronized static private void loadProps(){
    
            props = new Properties();
            InputStream in = null;
            try {
                in = PropertyUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
                props.load(in);
            } catch (FileNotFoundException e) {
    
            } catch (IOException e) {
    
            } finally {
                try {
                    if(null != in) {
                        in.close();
                    }
                } catch (IOException e) {
     
                }
            }
    
    
        }
    
        public static String getProperty(String key){
            if(null == props) {
                loadProps();
            }
            return props.getProperty(key);
        }
    
        public static String getProperty(String key, String defaultValue) {
            if(null == props) {
                loadProps();
            }
            return props.getProperty(key, defaultValue);
        }
    }

    第二种方式:

       通过spring注入的方式  

       通过参入注入即可

        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
            <property name="locations">  
                <list>  
                     <value>/WEB-INF/classes/dbconfig.properties</value>  
                     <value>classpath:redisconfig.properties</value>
                     <value>classpath:postconfig.properties</value>
                </list>  
            </property>  
        </bean> 
        <bean class="com.skjd.bean.PostCongfig">
                <property name="posturl" value="${posturl}" />  
        </bean>
  • 相关阅读:
    一个误解: 单个服务器程序可承受最大连接数“理论”上是“65535”
    Memcached 命令简介
    MySQL性能测试
    WCF 面向服务的SOAP消息
    WCF SOAP消息剖析
    深入探析 socket
    C#设计模式(适配器模式)
    LoadRunner中的异常处理
    反射调用性能比较(附源码)
    避免 TCP/IP 端口耗尽
  • 原文地址:https://www.cnblogs.com/qq376324789/p/10309253.html
Copyright © 2011-2022 走看看