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.<init>(AnnotationConfigApplicationContext.java:84)
    at com.yubai.EL.MainEl.main(MainEl.java:7)

这里写图片描述

分析思路

在路劲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();
    }
}

这里写图片描述

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值