我创建了一个简单的程序来检查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();
}
}
}
}
如果ClassPathXmlAppContext工作正常,我无法理解为什么在classpathResource.getInputStream()期间我得到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:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="src" path="src/test/java"/>
<classpathentry kind="src" path="src/test/resources"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry exported="true" kind="con" path="org.springsource.ide.eclipse.gradle.classpathcontainer"/>
<classpathentry kind="output" path="bin"/>
</classpath>
我试图检测系统性的java.class.path,我有春天的{project} / bin和jars,就是这样。没有资源文件夹。 当我访问src / main / resources / spring-config.xml时,如何从src / main / resources访问资源?
答案 0 :(得分:3)
您不需要添加&#39;类路径:&#39;使用构造函数时的前缀。这有效:
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();
}
}
}
}
答案 1 :(得分:0)
问题是vanilla Java,你运行它的方式不会将src / main / resources添加到类路径中,这是其他运行者使用的惯例。
以下工作正常。
package com.greg;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = { "classpath:spring-config.xml",
"classpath:testfile02.xml" })
public class ClasspathTest {
@Test
public void test1() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:spring-config.xml");
ClassPathResource testfile02 = new ClassPathResource("testfile02.xml");
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();
}
}
}
}