class path resource cannot be opened because it does not exits

E:\SoftwareTools\Java\jdk\bin\java ...
八月 12, 2017 10:42:56 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3f91beef: startup date [Sat Aug 12 22:42:56 CST 2017]; root of context hierarchy
八月 12, 2017 10:42:57 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext refresh
警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.yubai.EL.ELConfig]; nested exception is java.io.FileNotFoundException: class path resource [com/yubai/EL/test.properties] cannot be opened because it does not exist
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.yubai.EL.ELConfig]; nested exception is java.io.FileNotFoundException: class path resource [com/yubai/EL/test.properties] cannot be opened because it does not exist
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:181)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308)
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:270)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:93)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.(AnnotationConfigApplicationContext.java:84)
    at com.yubai.EL.MainEl.main(MainEl.java:7)

class path resource cannot be opened because it does not exits_第1张图片

分析思路

在路劲com/yubai/EL下存在此文件,但错误信息输出如下:

class path resource [com/yubai/EL/test.properties] cannot be opened because it does not exist

从错误信息可以看出
1. com/yubai/EL/test.properties 这个文件不存在,但从图片中可以看出,此文件是存在的,可想而知,IDEA不是在此文件下查找的,所以test.properties不应该放在此目录下。
2. 再查看错误信息,可以看出class path resource ,是在resource文件下查找的,在此resource文件夹下没有com/yubai/EL/test.properties,所以当我尝试在resource下创建此目录与文件后(com/yubai/EL/test.properties),再次执行就可以运行了

代码块

package com.yubai.EL;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;


@Configuration
@PropertySource("classpath:com/yubai/EL/test.properties")
public class ELConfig {

    @Value("classpath:com/yubai/EL/test.txt")
    private Resource testFile;

    @Value("${book.name}")
    private String bookName;


    @Bean
    public static PropertySourcesPlaceholderConfigurer propertyConfigurer(){
        return new PropertySourcesPlaceholderConfigurer();
    }

   public void output(){
        try{
            System.out.println(bookName);
            System.out.println(IOUtils.toString(testFile.getInputStream()));
        }catch (Exception e){
            e.printStackTrace();
        }
   }
}

Main.java

package com.yubai.EL;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainEl {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ELConfig.class);
        ELConfig resource = context.getBean(ELConfig.class);
        resource.output();
        context.close();
    }
}

class path resource cannot be opened because it does not exits_第2张图片

你可能感兴趣的:(spring)