Spring的Resource加载本地文件报错cannot be opened because it does not exist的解释

简介:

eclipse的maven项目通过pom导入spring的支持,使用org.springframework.core.io.Resource去调用spring容器本身的资源,比如,读取项目路径下的某一个txt文件的内容。
加载过程中报错:
image
这是为什么呢?我们看看eclipse中项目的文件结构
image
读取本地hosts.txt文件的代码如下:

package com.springbootaware.aware.service;

import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

@Service
public class AwareService implements BeanNameAware, ResourceLoaderAware {
    
    private String beanName;
    private ResourceLoader loader;

    @Override
    public void setResourceLoader(ResourceLoader arg0) {
        // TODO Auto-generated method stub

        this.loader = arg0;
    }

    @Override
    public void setBeanName(String arg0) {
        // TODO Auto-generated method stub

        this.beanName = arg0;
    }

    
    public void outputResult() {
        
        System.out.println("bean的名字:" + beanName);
        Resource r1 = loader.getResource("classpath:hosts.txt");
        try {
            System.out.println("ResourceLoader加载的文件:" + IOUtils.toString(r1.getInputStream()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
}

Resource r1 = loader.getResource("classpath:hosts.txt");
我们把hosts.txt文件放在了src文件夹下,这很显然是错误的。
我们需要把被读取的文件放在.class文件目录结构中
image
然后运行代码读取txt文件
image

目录
相关文章
|
3月前
|
缓存 负载均衡 Java
Spring Cloud Alibaba client升级问题之升级报错如何解决
Spring Cloud Alibaba提供了一套在Spring Cloud框架基础上构建的微服务解决方案,旨在简化分布式系统的开发和管理;本合集将探讨Spring Cloud Alibaba在实际应用中的部署和使用技巧,以及该框架常见问题的诊断方法和解决步骤。
|
1月前
|
Java Apache vr&ar
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
springmvc报错 nested exception is org.mybatis.spring.MyBatisSystemException:
16 0
|
1月前
|
Java Windows Perl
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
mybatis+spring报错PropertyAccessException 1: org.springframework.beans.MethodInvocationException
12 0
|
1月前
|
前端开发 Java Spring
ssm中spring mvc找不到控制器,报错404
ssm中spring mvc找不到控制器,报错404
14 0
|
1月前
|
XML Java 数据格式
解释Spring中一个bean的注入过程
解释Spring中一个bean的注入过程
|
2月前
|
安全 Java 开发者
Spring依赖注入大揭秘:@Autowired、@Qualifier和@Resource的区别与应用
Spring依赖注入大揭秘:@Autowired、@Qualifier和@Resource的区别与应用
60 0
|
2月前
|
Java Spring
spring-doc报错Unable to render this definition
spring-doc报错Unable to render this definition
165 0
|
2月前
spring3 springfox报错Type javax.servlet.http.HttpServletRequest not present
spring3 springfox报错Type javax.servlet.http.HttpServletRequest not present
41 0
|
3月前
|
存储 Java Maven
Spring Cloud Alibaba服务问题之服务报错如何解决
Spring Cloud Alibaba提供了一套在Spring Cloud框架基础上构建的微服务解决方案,旨在简化分布式系统的开发和管理;本合集将探讨Spring Cloud Alibaba在实际应用中的部署和使用技巧,以及该框架常见问题的诊断方法和解决步骤。
|
3月前
|
Java Spring
spring注解@Autowired、@Resource说明
spring注解@Autowired、@Resource说明