Menu

can't locate applicationContext.xml

Hung Tang
2003-09-03
2003-09-03
  • Hung Tang

    Hung Tang - 2003-09-03

    I'm trying out the Spring framework for the first time and I've been running to problems of locating the applicationContext.xml.  I want to test the bean factory capabilities, in particular I want to see what's IoC is all about.  But on the way, when compiling using XmlBeanFactory, my program complains that it can't find the applicationContext.xml.  Below is the error message I'm getting:

    Sep 2, 2003 9:24:41 PM org.springframework.beans.factory.xml.XmlBeanFactory loadBeanDefinitions

    INFO: Loading XmlBeanFactory from file 'applicationContext.xml'
    java.io.FileNotFoundException: applicationContext.xml (The system cannot find the file specified)
    at java.io.FileInputStream.open(Native Method)
    ...

    My code looks something like this:
    AbstractBeanFactory bean = new XmlBeanFactory("applicationContext.xml");

    I even tried adding "/" to it, and also trying
    various constructors that's part of XmlBeanFactory
    to no avail. 

    I am doing this is as a standalone test so I am
    not bounded to any web application at this moment.

    Has anyone experienced similiar problems?

    Thanks

     
    • Thomas Risberg

      Thomas Risberg - 2003-09-03

      SInce you are not specifying a path for the xml file you are using, then it must be in the same directory where you execute your program from - not inside any of your package directories.  That would be at the root of your package hierarchy.

      As an alternative you could try to specify the full path for the xml file.

      Just adding "/" in front of the file name would only help if the file was loaded from the class path, but that does not appear to be the case.

      Thomas

       
    • Rod Johnson

      Rod Johnson - 2003-09-03

      The XmlBeanFactory constructor that takes a string expects a system filename as recognized by java.io.FileInputStream, like "c:\\work\\project1\\myfile.xml" on Windows. FileInputStream doesn't know where to look for "applicationContext.xml" as in your code. So the exception is correct.

      Loading from the classpath is often a better idea than loading from the filesystem (no dependency on your OS or directory structure). To do this, get an InputStream from the desired resource and use it to construct the XmlBeanFactory. This will look for applicationContext.xml in the current directory:

      InputStream is = getClass().getResourceAsStream("applicationContext.xml");
      XmlBeanFactory xbf = new XmlBeanFactory(is);

      Prepending a / will make it look in the base of the classpath.

      In this case the resource lookup is done by the java.io package not Spring. Check that the InputStream is not null as this will indicate it wasn't found (there will be no exception).

      Regards,
      Rod

       
      • Hung Tang

        Hung Tang - 2003-09-03

        Rod,

        I got the problem fixed with your explanation.

        thank you very much.

         
        • Dmitriy Kopylenko

          Or you could use it directly like this:

          AbstractBeanFactory bean = new ClassPathXmlApplicationContext("applicationContext.xml");

          Regards,
          Dmitriy.

           

Log in to post a comment.