Mat*_*aga 23 java classloader kotlin
我正在将项目迁移到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;
}
Run Code Online (Sandbox Code Playgroud)
就是现在:
fun provideProperties(propertiesFileName: String): Properties? {
return Properties().apply {
ObjectFactory::class.java.classLoader.getResourceAsStream(propertiesFileName).use { stream ->
load(stream)
}
}
}
Run Code Online (Sandbox Code Playgroud)
非常好,Kotlin!:P
问题是:这个方法.properties在里面查找文件src/main/resources.使用:
ObjectFactory::class.java.classLoader...
Run Code Online (Sandbox Code Playgroud)
它有效,但使用:
this.javaClass.classLoader...
Run Code Online (Sandbox Code Playgroud)
classLoader是null......
(注意内存地址也不同)
为什么?
谢谢
| 归档时间: |
|
| 查看次数: |
6347 次 |
| 最近记录: |