Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java项目:无法访问资源目录中的属性文件_Java_Spring_Jersey - Fatal编程技术网

Java项目:无法访问资源目录中的属性文件

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

我正在构建一个jersey-2网络应用程序。我无法从资源访问config.property文件。我错过了什么

@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;
}