2012-10-05 24 views
1

我在同一个JVM中有两个maven项目AB。项目B取决于项目A。在一类项目A中,我需要加载项目B的属性文件,例如/B/src/main/resource/foo.properties在静态块是类似以下内容:从同一个JVM中的另一个项目加载属性文件

Class bar{//this class is in project A 
    static{ 
    //do the magic to load the property file in project B 
    } 
    } 

什么是访问提到的属性文件项目B正确的道路?

请帮助一个工作伪语句。提前致谢!!

+0

IF,相关资源捆绑(即内的JAR文件)或withing两个项目的类路径中,你应该能够使用'bar.class.getResource (“/resource/foo.properties”),它将返回一个URL到指定的资源。这可能需要您构建有问题的项目 – MadProgrammer

回答

0

由于您已经指定了maven依赖关系,所有classpath资源都是另一个项目可用的另一个项目。

试试这个代码

public void doSomeOperation() throws IOException { 
     // Get the inputStream 
     InputStream inputStream = Bar.class.getClassLoader() 
       .getResourceAsStream("myApp.properties"); 

     Properties properties = new Properties(); 

     System.out.println("InputStream is: " + inputStream); 

     // load the inputStream using the Properties 
     properties.load(inputStream); 
     // get the value of the property 
     String propValue = properties.getProperty("abc"); 

     System.out.println("Property value is: " + propValue); 
    } 
相关问题