`
fantaxy025025
  • 浏览: 1252265 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

jar包内存在同名同路径的配置文件,会出问题吗?

 
阅读更多

 

问题:

#两个jar包,a.jar,   b.jar

#各有一个配置文件,同名同路径,都是conf/db.properties,内容*不*同。

#这俩jar包都会读取*其jar内*的此配置文件

问题是:会出现混淆问题么?

 

答案:

# 如何读取jar保内的配置文件。

# jvm的classloader加载文件的顺序。

# 加载.class文件和其他文件有区别吗?

 

其他:

一定要解决此问题的,否则多个module依赖会出现很大的问题。

 

工程:

a.jar

public class TestAaa {

    public static void main(String[] args) {
        TestAaa.readDbProp();
        System.out.println("----");
        TestAaa.scanProps();
    }

    public static void readDbProp() {
        //返回读取指定资源的输入流
        InputStream is = TestAaa.class.getResourceAsStream("/conf/db.properties");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String s = "";
        try {
            while ((s = br.readLine()) != null){
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void scanProps(){
        Enumeration<URL> ps = null;
        URL url = null;
        try {
            ps = Thread.currentThread().getContextClassLoader().getResources("conf/db.properties");
            System.out.println("ps.hasMoreElements()?" + ps.hasMoreElements());
//            url = Thread.currentThread().getContextClassLoader().getResource("/conf/db.properties");

        } catch (IOException e) {
            e.printStackTrace();
        }

        while(ps.hasMoreElements()) {
            InputStream in = null;
            try {
                in = ps.nextElement().openStream();
                Properties p = new Properties();
                p.load(in);
                System.out.println(p.getProperty("username"));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                closeIO(in);
            }
        }
    }

    private static void closeIO(Closeable io) {
        if(io != null) {
            try {
                io.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

 

b.jar

public class TestAaa {

    public static void main(String[] args) {
        TestAaa.readDbProp();
        System.out.println("----");
        TestAaa.scanProps();
    }

    public static void readDbProp() {
        //返回读取指定资源的输入流
        InputStream is = TestAaa.class.getResourceAsStream("/conf/db.properties");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String s = "";
        try {
            while ((s = br.readLine()) != null){
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void scanProps(){
        Enumeration<URL> ps = null;
        URL url = null;
        try {
            ps = Thread.currentThread().getContextClassLoader().getResources("conf/db.properties");
            System.out.println("ps.hasMoreElements()?" + ps.hasMoreElements());
//            url = Thread.currentThread().getContextClassLoader().getResource("/conf/db.properties");

        } catch (IOException e) {
            e.printStackTrace();
        }

        while(ps.hasMoreElements()) {
            InputStream in = null;
            try {
                in = ps.nextElement().openStream();
                Properties p = new Properties();
                p.load(in);
                System.out.println(p.getProperty("username"));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                closeIO(in);
            }
        }
    }

    private static void closeIO(Closeable io) {
        if(io != null) {
            try {
                io.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

 

main.jar

public class TestMain {

    public static void main(String[] args) {
//        TestBbb.readDbProp();
        System.out.println("+++++++++++++++++++++");
        TestAaa.readDbProp();

//        TestBbb.scanProps();
        System.out.println("+++++++++++++++++++++");
        TestAaa.scanProps();
    }

}

 测试的时候注意调整jar包的顺序。

 

# 可以读取多个同名配置文件

    public static void scanProps(){
        Enumeration<URL> ps = null;
        URL url = null;
        try {
            ps = Thread.currentThread().getContextClassLoader().getResources("conf/db.properties");
            System.out.println("ps.hasMoreElements()?" + ps.hasMoreElements());
        } catch (IOException e) {
            e.printStackTrace();
        }

        while(ps.hasMoreElements()) {
            InputStream in = null;
            try {
                in = ps.nextElement().openStream();
                Properties p = new Properties();
                p.load(in);
                System.out.println(p.getProperty("username"));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                closeIO(in);
            }
        }
    }

 

 

# 读取单个和多个文件的路径写法

Thread.currentThread().getContextClassLoader().getResources("conf/db.properties");

 

TestBbb.class.getResourceAsStream("/conf/db.properties");

 

P

a

a

e

e

r

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics