I am trying to load a XML file into a input stream with spring-mvc's ApplicationContext
. But I am not able to read the file and I am getting conflicting information.
I am trying to load a xml file into a InputStream
and find the file via:
ApplicationContext ctx =
new ClassPathXmlApplicationContext(new String[]{"classpath*:config/validation.xml"});
I then what to initialize my InputStream
from the ApplicantionContext
but I am unclear how to. Right now I have:
private InputStream forms = getClass().getClassLoader().getResourceAsStream("/validation.xml");
However the issue at the moment is I cannot even find the file.
Conflicting information:
I am put the files under resources and again in webapp-> WEB-INF-> config
to try to figure out where it could be loaded from.
ApplicationContext ctx =
new ClassPathXmlApplicationContext(new String[]{"classpath*:config/validation.xml"});
Say FOUND!!!!!!!!!!!
public Validator() {
this.testResource();
if(ctx != null){
System.out.println("FOUND!!!!!!!!!!!!!!!!!!!!"+ctx); //this prints
}else{
System.out.println("NOT FOUND");
}
}
Output:
FOUND!!!!!!!!!!!!!!!!!!!!org.springframework.beans.factory.support.DefaultListableBeanFactory@635bc5cc: defining beans []; root of factory hierarchy
However this says the file cannot be found:
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();
}
}
--------------------------Update 1-----------------------
I now have the following and it too is giving me conflicting information saying found then an 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);
}
}
}
Output:
FOUND!!!!!!!!!!!!!!!!!!!!class path resource [classpath*:validation.xml] runInputStream IOException: java.io.FileNotFoundException: class path resource [classpath*:validation.xml] cannot be opened because it does not exist