src main java6_java-src / main / resources中的文件不在类路径中

我创建了一个简单的程序来检查spring-context是否在src / main / resources文件夹中定义了一个文件.

我有这个文件结构:

project

--> src/main/resources/spring-config.xml

--> src/main/resources/testfile02

我尝试使用此测试类访问这些文件

public class ClasspathTest {

public static void main(String args[]) throws URISyntaxException {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-config.xml");

ClassPathResource testfile02 = new ClassPathResource("classpath:testfile02");

if (testfile02 != null) {

try (InputStream inputStream = testfile02.getInputStream();

Reader streamReader = new InputStreamReader(inputStream, "UTF-8");

BufferedReader bufferedReader = new BufferedReader(streamReader)) {

String line;

while ((line = bufferedReader.readLine()) != null) {

System.out.println(line);

}

} catch (IOException exc) {

exc.printStackTrace();

}

}

}

}

而且我不明白为什么在ClasspathResource.getInputStream()期间,如果ClassPathXmlAppContext运行正常,为什么会出现FileNotFound异常.

执行日志:

янв 19, 2016 11:57:48 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6193b845: startup date [Tue Jan 19 11:57:48 MSK 2016]; root of context hierarchy

янв 19, 2016 11:57:48 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

INFO: Loading XML bean definitions from class path resource [spring-config.xml]

java.io.FileNotFoundException: class path resource [classpath:testfile02] cannot be opened because it does not exist

该项目是使用gradle构建的:

apply plugin: 'java'

apply plugin: 'eclipse'

sourceCompatibility = 1.8

version = '1.0'

repositories {

mavenCentral()

}

dependencies {

compile 'org.springframework:spring-context:4.1.6.RELEASE'

}

.classpath:

我试图检测systemproperty java.class.path,并且在那里有{project} / bin和jars作为弹簧,仅此而已.没有资源文件夹.

如何在访问src / main / resources / spring-config.xml时从src / main / resources访问资源?

解决方法:

使用构造函数时,无需添加“ classpath:”前缀.这有效:

public class ClasspathTest {

public static void main(String args[]) throws URISyntaxException {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-config.xml");

ClassPathResource testfile02 = new ClassPathResource("testfile02");

if (testfile02 != null) {

try (InputStream inputStream = testfile02.getInputStream();

Reader streamReader = new InputStreamReader(inputStream, "UTF-8");

BufferedReader bufferedReader = new BufferedReader(streamReader)) {

String line;

while ((line = bufferedReader.readLine()) != null) {

System.out.println(line);

}

} catch (IOException exc) {

exc.printStackTrace();

}

}

}

}

标签:spring,java

来源: https://codeday.me/bug/20191119/2033962.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值