我迁移项目科特林,这:科特林: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