Java 无法将带有ApplicationContext的XML文件读入InputStream
我正在尝试使用SpringMVC的Java 无法将带有ApplicationContext的XML文件读入InputStream,java,xml,spring-mvc,Java,Xml,Spring Mvc,我正在尝试使用SpringMVC的ApplicationContext将XML文件加载到输入流中。但我无法阅读该文件,我得到了相互矛盾的信息 我正在尝试将xml文件加载到InputStream中,并通过以下方式查找该文件: ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"classpath*:config/validation.xml"}); 然后,我从applicationcontext初
ApplicationContext
将XML文件加载到输入流中。但我无法阅读该文件,我得到了相互矛盾的信息
我正在尝试将xml文件加载到InputStream
中,并通过以下方式查找该文件:
ApplicationContext ctx =
new ClassPathXmlApplicationContext(new String[]{"classpath*:config/validation.xml"});
然后,我从applicationcontext
初始化我的InputStream
,但我不清楚如何初始化。现在我有:
private InputStream forms = getClass().getClassLoader().getResourceAsStream("/validation.xml");
然而,目前的问题是,我甚至找不到该文件
冲突信息:
我将这些文件放在参考资料下,然后再次放在webapp->WEB-INF->config
中,试图找出可以从何处加载这些文件
ApplicationContext ctx =
new ClassPathXmlApplicationContext(new String[]{"classpath*:config/validation.xml"});
说找到了代码>
public Validator() {
this.testResource();
if(ctx != null){
System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+ctx); //this prints
}else{
System.out.println("NOT FOUND");
}
}
输出:
找到了!!!!!!!!!!!!!!!!!!!!org.springframework.beans.factory.support。DefaultListableBeanFactory@635bc5cc:定义bean[];工厂层次结构的根
但是,这表示找不到该文件:
private void testResource(){
Resource resource =
this.ctx.getResource("classpath*:/validation.xml");
try{
InputStream is = resource.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
}catch(IOException e){
System.out.println("ERROR it is not found: ");
e.printStackTrace();
}
}
--------------------------更新1-----------------------
我现在有以下信息,它也给了我矛盾的信息,说发现了IOException:
@Configuration
public class Validator {
private InputStream forms;
private ValidatorResources resources;
ApplicationContext ctx =
new ClassPathXmlApplicationContext(new String[]{"classpath*:config/validation.xml"});
private Resource resource = this.ctx.getResource("classpath*:validation.xml");
public Validator() {
if(resource != null){
System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+resource);
}else{
System.out.println("NOT FOUND");
}
this.runInputStream();
}
private void runInputStream(){
try{
this.forms = this.resource.getInputStream();
this.resources = new ValidatorResources(this.forms);
}catch (IOException ex) {
System.out.println("runInputStream IOException: "+ex);
}catch (SAXException e) {
System.out.println("runInputStream SAXException: "+e);
}catch (Exception e){
System.out.println("ERROR-------------"+e);
}
}
}
输出:
找到了!!!!!!!!!!!!!!!!!!!!类路径资源
[classpath*:validation.xml]runInputStream IOException:
java.io.FileNotFoundException:类路径资源
无法打开[classpath*:validation.xml],因为它不存在
您应该删除/
:
getClass().getClassLoader().getResourceAsStream("validation.xml");
并且validation.xml文件必须位于resources文件夹的根目录下
如果一切正常,构建项目后,您可以在WEB-INF/classes中找到validation.xml文件,因此它可以在类加载器中找到。我认为您需要将该文件加载为:
Resource resource =
this.ctx.getResource("classpath*:validation.xml");
因此,我们加载该文件时,假设它位于类路径上的任何位置我将它放在我的资源文件的根目录下,并且在webapp->config->
中,我收到一个错误,上面写着error--------------java.lang.IllegalArgumentException:Stream[0]为null
当我执行private InputStream forms=getClass().getClassLoader()时.getResourceAsStream(“validation.xml”)代码>我这样做并尝试调试程序,当我打印到控制台时,调试器会说类为null:System.out.println(“资源:+RESOURCE.getFile().getAbsolutePath())代码>错误:IOException:java.io.FileNotFoundException
嗯,我不知道,但是你在运行一个webapp吗?应通过ServletContext?访问WEB-INF。你能把文件放在src/main/resources中吗?我希望在那里可以访问任何旧文件。@KarlNicholas我在参考资料和WEB-INF中找到了它。我正在试图找出它找不到的原因。