Results 1 to 5 of 5

Thread: javac can't find XmlBeanFactory

  1. #1

    Default javac can't find XmlBeanFactory

    here is the classpath:

    C:\>set classpath=.;C:\;D:\;
    C:\SpringFramework-2.0\dist\spring.jar;
    C:\spring-framework-2.0\lib\jakarta-commons\commons-logging.jar;
    C:\com\springinaction\chapter01\hello\

    here is the compilation command

    C:\>javac com\springinaction\chapter01\hello\HelloApp.java


    here is the result - can't find the XmlBeanFactory

    com\springinaction\chapter01\hello\HelloApp.java:1 1: cannot find symbol
    symbol : constructor XmlBeanFactory(java.io.FileInputStream)
    location: class org.springframework.beans.factory.xml.XmlBeanFacto ry
    BeanFactory factory = new XmlBeanFactory(new FileInputStream("hello.xml"));
    ^
    1 error

    C:\>

    Anyone have an idea what is wrong?

    Thanks,

    Bill

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Well, the class XmlBeanFactory has been found, actually. What is missing is the constructor taking an InputStream, and indeed such a constructor does not exist (at least not in recent versions of Spring).

    You may replace the line equivalently with
    Code:
    BeanFactory factory = new XmlBeanFactory(new InputStreamResource(new FileInputStream("hello.xml")));
    Regards,
    Andreas

  3. #3

    Default

    Thanks for your help, Andreas

    I added the import for InputStreamResource and the program runs.
    It does complain about not finding org/apache/commons/logging/LogFactory
    but I will work on that for a while. I beleive I need to download another
    software package.

  4. #4

    Default

    Well, I found that I needed additional jar in the classpath to support logging.
    And now it emits another error, see below:

    runtime:

    C:\>java com.springinaction.chapter01.hello.HelloApp
    30-Jan-2007 12:35:07 PM org.springframework.core.CollectionFactory <clinit>
    INFO: JDK 1.4+ collections available
    30-Jan-2007 12:35:07 PM org.springframework.beans.factory.xml.XmlBeanDefin itionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from resource loaded through InputStream
    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionSt oreException: Passed-in Resource [resource loaded throu
    gh InputStream] contains an open stream: cannot determine validation mode automatically. Either pass in a Resource that is able to c
    reate fresh streams, or explicitly specify the validationMode on your XmlBeanDefinitionReader instance.
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.detectValidationMode(XmlBeanDefinition Reader.java:445)
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.getValidationModeForResource(XmlBeanDe finitionReader.java:4
    27)
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.doLoadBeanDefinitions(XmlBeanDefinitio nReader.java:387)
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:340)
    at org.springframework.beans.factory.xml.XmlBeanDefin itionReader.loadBeanDefinitions(XmlBeanDefinitionR eader.java:317)
    at org.springframework.beans.factory.xml.XmlBeanFacto ry.<init>(XmlBeanFactory.java:73)
    at org.springframework.beans.factory.xml.XmlBeanFacto ry.<init>(XmlBeanFactory.java:61)
    at com.springinaction.chapter01.hello.HelloApp.main(H elloApp.java:14)

    C:\>

    sourcefile:
    package com.springinaction.chapter01.hello;

    import java.io.FileInputStream;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFacto ry;
    import org.springframework.core.io.InputStreamResource;

    public class HelloApp
    {
    public static void main(String[] args) throws Exception
    {
    FileInputStream fs = new FileInputStream("hello.xml");
    InputStreamResource isr = new InputStreamResource(fs);
    BeanFactory factory = new XmlBeanFactory(isr);
    //BeanFactory factory = new XmlBeanFactory(new InputStreamResource(new FileInputStream("hello.xml")));
    //BeanFactory factory = new XmlBeanFactory(new FileInputStream("hello.xml"));

    GreetingService greetingService = (GreetingService) factory.getBean("greetingService");
    greetingService.sayGreeting();
    }
    }


    I interpret this to be a problem with the 'hello.xml' file being open. But I am
    not sure what is meant by open in this context - the file is not open by any
    other applications. The exception is being thrown by the statement:

    BeanFactory factory = new XmlBeanFactory(isr);

    which is passed in the filename as a resource.

    Any ideas where to go next?

    Thanks in advance,

    Bill.

  5. #5

    Default

    I found the problem. Looking at the code for XmlBeanFactory showed that
    it throws the exception I was seeing if it is passed an open file and
    the function FileInputStream opens the file. Changing to another
    function - FileSystemResource - looks like it returns the file as a resource
    but does not open it. The modified code is below...

    package com.springinaction.chapter01.hello;

    import java.io.FileInputStream;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFacto ry;
    import org.springframework.core.io.FileSystemResource;

    public class HelloApp
    {
    public static void main(String[] args) throws Exception
    {
    FileSystemResource fsr = new FileSystemResource("hello.xml");
    BeanFactory factory = new XmlBeanFactory(fsr);
    GreetingService greetingService = (GreetingService)factory.getBean("greetingService" );

    greetingService.sayGreeting();
    }
    }

    And the output...


    C:\>java com.springinaction.chapter01.hello.HelloApp
    30-Jan-2007 1:17:27 PM org.springframework.core.CollectionFactory <clinit>
    INFO: JDK 1.4+ collections available
    30-Jan-2007 1:17:27 PM org.springframework.beans.factory.xml.XmlBeanDefin itionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from file [C:\\hello.xml]
    Buenos Dias!

    C:\>

    So now a working example I can understand.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •