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>
  • 相关阅读:
    《MySQL入门很简单》练习7.4
    《MySQL入门很简单》练习6.9
    《MySQL入门很简单》练习6.6
    《MySQL入门很简单》练习6.5
    "mysql"不是内部或外部命令,也不是可运行的程序或批处理文件
    TControl与Windows消息
    TObject与消息分发
    长串
    使用TSplitter控件调整其他控件大小简便方法
    Cocos2d-x缓存机制(一)
  • 原文地址:https://www.cnblogs.com/qq376324789/p/10309253.html
Copyright © 2011-2022 走看看