Kotlin: MyClass::class.java vs this.javaClass
2022-09-01 14:43:17
我正在将一个项目迁移到 Kotlin,这个:
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
问题是:此方法在 里面查找文件。用:.properties
src/main/resources
ObjectFactory::class.java.classLoader...
它的工作原理,但使用:
this.javaClass.classLoader...
classLoader
是。。。null
(注意内存地址也不同)
为什么?
谢谢