Java项目:无法访问资源目录中的属性文件
我正在构建一个jersey-2网络应用程序。我无法从资源访问config.property文件。我错过了什么Java项目:无法访问资源目录中的属性文件,java,spring,jersey,Java,Spring,Jersey,我正在构建一个jersey-2网络应用程序。我无法从资源访问config.property文件。我错过了什么 @Bean(name = com.tarkshala.photo.drive.Configuration.CONFIGURATION_BEAN_NAME) public Properties getConfiguration() throws IOException { Properties properties = new Properties(); InputStre
@Bean(name = com.tarkshala.photo.drive.Configuration.CONFIGURATION_BEAN_NAME)
public Properties getConfiguration() throws IOException {
Properties properties = new Properties();
InputStream input = new FileInputStream("config.properties");
properties.load(input);
return properties;
}
Bean初始化失败,出现以下错误:
Caused by: java.lang.NullPointerException
at com.tarkshala.photo.spring.SpringAppConfig.getConfiguration(SpringAppConfig.java:47)
at com.tarkshala.photo.spring.SpringAppConfig$$EnhancerBySpringCGLIB$$3ecfb6f6.CGLIB$getConfiguration$5(<generated>)
at com.tarkshala.photo.spring.SpringAppConfig$$EnhancerBySpringCGLIB$$3ecfb6f6$$FastClassBySpringCGLIB$$45328ae1.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
原因:java.lang.NullPointerException
位于com.tarkshala.photo.spring.SpringAppConfig.getConfiguration(SpringAppConfig.java:47)
在com.tarkshala.photo.spring.SpringAppConfig$$EnhancerBySpringCGLIB$$3ecfb6f6.CGLIB$getConfiguration$5()
在com.tarkshala.photo.spring.SpringAppConfig$$EnhancerBySpringCGLIB$$3ecfb6f6$$FastClassBySpringCGLIB$$45328ae1.invoke()上
位于org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
位于org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358)
您可以尝试使用CurrentClass.class.getClassLoader().getResourceAsStream(“config.properties”)主要区别在于,在类加载器实例上使用getResourceAsStream时,从类路径的根开始,路径被视为绝对路径
“虽然此代码可以回答问题,但最好在此处包含答案的基本部分,并提供链接以供参考。
public InputStream getResourceFileAsInputStream(String fileName) {
ClassLoader classloader = SpringAppConfig.class.getClassLoader();
return classloader.getResourceAsStream(fileName);
}
public Properties getConfiguration() throws IOException {
Properties properties = new Properties();
InputStream input = getResourceFileAsInputStream("config.properties");
properties.load(input);
return properties;
}