Results 1 to 6 of 6

Thread: how to use XmlBeanFactory?

  1. #1
    Join Date
    Jun 2006
    Posts
    3

    Default how to use XmlBeanFactory?

    i follow the code in "spring in action"
    but it's told me:

    C:\user\src>javac hello\HelloApp.java
    hello\HelloApp.java:12: 找不到符号
    符号: 构造函数 XmlBeanFactory(java.io.FileInputStream)
    位置: 类 org.springframework.beans.factory.xml.XmlBeanFacto ry
    BeanFactory factory = new XmlBeanFactory(new FileInputStream("hello.xml"))
    ;
    ^

    how to use XmlBeanFactory?

    i already set the spring in classpath:
    C:\user\src>echo %classpath%
    .;C:\Program Files\Java\jdk1.5.0_07\lib\tools.jar;C:\user\softw are-for-install\j
    2ee-software\spring-framework-1.2.8-with-dependencies\spring-framework-1.2.8\dis
    t\spring.jar;

  2. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Not sure what your error is (I only see question marks) but here is an example:
    XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("proxyFactoryBean.xml", getClass()));
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Jun 2006
    Posts
    3

    Default

    this is my program:
    =================
    package hello;

    public interface GreetingService{
    public void sayGreeting();
    }
    =================
    package hello;

    public class GreetingServiceImpl implements GreetingService{
    private String greeting;
    public GreetingServiceImpl(){
    }
    public GreetingServiceImpl(String greeting){
    this.greeting = greeting;
    }


    public void sayGreeting() {
    System.out.println(greeting);
    }
    public void setGreeting(String greeting){
    this.greeting = greeting;
    }
    }
    =================
    package hello;

    import java.io.FileInputStream;

    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFacto ry;
    import org.springframework.beans.factory.support.DefaultL istableBeanFactory;
    import org.springframework.core.io.Resource;

    public class HelloApp{
    public static void main(String[] args) throws Exception{
    XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("hello.xml", getClass()));
    GreetingService greetingService = (GreetingService) factory.getBean("greetingService");
    greetingService.sayGreeting();
    }
    }
    ===========================
    still can not run

    the problem in: XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("hello.xml", getClass()));


    the book use spring 1.1.3, but now i use the newest spring.
    maybe some api change?

    how can i do?
    thanks

  4. #4
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Without any error/stacktrace I can't tell what is the problem. First identify what Spring version are you using (see the one from the classpath) then check it's API.
    AFAIK, XmlBeanFactory hasn't changed that much since the 1.x days but I may be wrong - the example I just gave you is part of the 2.x source code(tests to be more precise).
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  5. #5
    Join Date
    Jun 2006
    Posts
    3

    Default

    i use spring-framework-1.2.8-with-dependencies

    the code in book like this:

    BeanFactory factory = new XmlBeanFactory(new FileInputStream("hello.xml"));

    the error info like the first i post!

    thanks

  6. #6
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    C:\user\src>javac hello\HelloApp.java
    hello\HelloApp.java:12: 找不到符号
    符号: 构造函数 XmlBeanFactory(java.io.FileInputStream)
    位置: 类 org.springframework.beans.factory.xml.XmlBeanFacto ry
    BeanFactory factory = new XmlBeanFactory(new FileInputStream("hello.xml"))
    ;
    ^
    There is no message above just question marks. Not sure what book are you using but you should check with the official site/editor if there is an errata.
    Anyway, as you can see from the 1.2.x and 2.0.x javadocs that XmlBeanFactory uses a Resource to load the context and not a stream. You're probably better off using Spring 1.1.3 as the book uses and then upgrade to 2.0.x. Note however, that 1.1.3 is an old version of Spring (almost 2 generations and 1.5 years old) so nowadays there isn't a lot of support for it.
    However, the upgrade path is quite easy, in most cases the Spring changelog offering plenty of information.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

Posting Permissions

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