第一种方式 :

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 >  
        <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>

 

相关文章:

  • 2021-08-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
  • 2021-08-23
猜你喜欢
  • 2021-10-28
  • 2022-12-23
  • 2021-08-12
  • 2022-01-17
  • 2022-12-23
相关资源
相似解决方案