1

私は 2 つの Maven プロジェクトAB同じ JVM に持っています。プロジェクトはプロジェクトBに依存しますA。project の 1 つのクラスでは、次のような static ブロックなどにAproject のプロパティ ファイルをロードする必要があります。B/B/src/main/resource/foo.properties

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

プロジェクトで言及されたプロパティ ファイルにアクセスするための正しいパスは何Bですか?

動作する疑似ステートメントを手伝ってください。前もって感謝します!!

4

1 に答える 1

0

すでにMavenの依存関係が指定されているため、すべてのクラスパスリソースは、別のプロジェクトから別のプロジェクトで利用できるようになっています。

このコードを試してください

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);
    }
于 2012-10-05T04:59:35.947 に答える