0
点赞
收藏
分享

微信扫一扫

java自定义类加载properties文件

凛冬已至夏日未远 2022-04-01 阅读 60
java

properties文件放在resourses文件下

name=123
age=1
address=南京
phone=123456
url=http://www.baidu.com
static Properties prop = null ;
public static void loadProperty(){
//3种方法加载resourses目录下的spring.properties文件
       // InputStream is = this.getClass().getClassLoader().getResourceAsStream("spring.properties");
       InputStream is1 = PropertiesUtil.class.getResourceAsStream("/spring.properties");
       // InputStream is1 = PropertiesUtil.class.getClassLoader().getResourceAsStream("spring.properties");
        try {
            prop = new Properties();
            prop.load(is1);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (is1 != null) {
                try {
                    is1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

public  static  Object getProperty(String key){
      if(prop == null){
            loadProperty();
      }
      return prop.get(key);
    }

this代表当前对象,不能在static方法中使用

举报

相关推荐

0 条评论