我遷移項目科特林,這:科特林:MyClass的:: class.java VS this.javaClass
public static Properties provideProperties(String propertiesFileName) {
Properties properties = new Properties();
InputStream inputStream = null;
try {
inputStream = ObjectFactory.class.getClassLoader().getResourceAsStream(propertiesFileName);
properties.load(inputStream);
return properties;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
現在是:
fun provideProperties(propertiesFileName: String): Properties? {
return Properties().apply {
ObjectFactory::class.java.classLoader.getResourceAsStream(propertiesFileName).use { stream ->
load(stream)
}
}
}
非常漂亮,科特林! :P
問題是:此方法在src/main/resources
內尋找.properties
文件。使用:
ObjectFactory::class.java.classLoader...
它的工作原理,但使用:
this.javaClass.classLoader...
classLoader
是null
...
(注意內存地址也不同)
爲什麼?
感謝
'this.javaClass'的值是什麼? – chrylis
我編輯了我的問題。 –
您是否在運行時或編譯時遇到錯誤? – chrylis