gpt4 book ai didi

java - 从同一 JVM 中的另一个项目加载属性文件

转载 作者:行者123 更新时间:2023-12-01 05:05:33 25 4
gpt4 key购买 nike

我在同一个 JVM 中有两个 Maven 项目 AB。项目 B 依赖于项目 A。在项目 A 的一类中,我需要加载项目 B 的属性文件,例如/B/src/main/resource/foo.properties 在静态 block 中,如下所示:

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

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

请帮助制作一个有效的伪语句。提前致谢!!

最佳答案

由于您已经指定了 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);
}

关于java - 从同一 JVM 中的另一个项目加载属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12739397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com