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>
  • 相关阅读:
    bzoj2763 [JLOI]飞行路线 分层图最短路
    [模板]分块/可修改莫队 (数颜色种类)
    gcd步数
    洛谷2378 因式分解 字符串
    bzoj1090 字符串折叠
    洛谷1034 NOIP2002 矩形覆盖
    Codeforces#441 Div.2 四*题
    SPFA的小优化
    洛谷1073 NOIP2009 最优贸易
    bzoj2100 [Usaco2010 DEC]Apple Delivery苹果贸易
  • 原文地址:https://www.cnblogs.com/qq376324789/p/10309253.html
Copyright © 2011-2022 走看看