Results 1 to 10 of 10

Thread: Multiple XML files for a BeanFactory?

  1. #1

    Default Multiple XML files for a BeanFactory?

    Is there a way to use multiple XML files with a BeanFactory? I know for ApplicationContexts like ClassPathXmlApplicationContext it is easy: just specify a list of files in the constructor. But an app context is too heavyweight for what I'm trying to do.

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    As of Spring 1.1.2, you can create a XML file that contains <import> tags to include your XML configuration files, then consume the "super" file using XMLBeanFactory.
    an app context is too heavyweight for what I'm trying to do.
    Could you say more on what yor are trying to achieve?
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3

    Default

    Ahh. Cool, thanks.

    What I'm trying to do is create a group of DAOs that can be pointed to a unique data source. I may need to create several such groups during the course of the JVM process and use them simultaneously, so I want to minimize the overhead of creating each group. (The DAOs won't be true singletons. That's fine.)

  4. #4

    Default

    Oops, I spoke too soon. That probably won't work for me because the XML files I need to combine aren't known until runtime. I have a "common" file and a DBMS type-specific file. I won't know which DBMS file to use until I create the BeanFactory.

  5. #5
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    You can construct dynamically the beans configuration, as a string, and use:
    Code:
      import java.io.StringBufferInputStream;
      import org.springframework.core.io.InputStreamResource;
      import org.springframework.beans.factory.xml.XmlBeanFactory;
    
      String config = "<beans>" +
                      "  <bean id=\"myBean"\ class="org.taha.SomeClass"/>
                      "</beans>";
    
      StringBufferInputStream sbis = new StringBufferInputStream&#40;config&#41;;
      InputStreamResource isr = new InputStreamResource&#40;sbis&#41;;
      XmlBeanFactory xml = new XmlBeanFactory&#40;isr&#41;;
    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  6. #6
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    Quote Originally Posted by irbouho
    You can construct dynamically the beans configuration, as a string, and use:
    Code:
      import java.io.StringBufferInputStream;
      import org.springframework.core.io.InputStreamResource;
      import org.springframework.beans.factory.xml.XmlBeanFactory;
    
      String config = "<beans>" +
                      "  <bean id=\"myBean"\ class="org.taha.SomeClass"/>
                      "</beans>";
    
      StringBufferInputStream sbis = new StringBufferInputStream&#40;config&#41;;
      InputStreamResource isr = new InputStreamResource&#40;sbis&#41;;
      XmlBeanFactory xml = new XmlBeanFactory&#40;isr&#41;;
    HTH
    If your're going to do this, you might as well just work with the real programmatic apis, its easier, i.e. create RootBeanDefinitions and the like...
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  7. #7
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    well, the example I gave was really bad, I should have given the following:
    Code:
      import java.io.StringBufferInputStream; 
      import org.springframework.core.io.InputStreamResource; 
      import org.springframework.beans.factory.xml.XmlBeanFactory; 
    
      String config = "<beans>" + 
                      "  <import resource=\"file1.xml\"/> 
                      "</beans>"; 
    
      StringBufferInputStream sbis = new StringBufferInputStream&#40;config&#41;; 
      InputStreamResource isr = new InputStreamResource&#40;sbis&#41;; 
      XmlBeanFactory xml = new XmlBeanFactory&#40;isr&#41;;
    as this best meet the initial need.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  8. #8
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default Re: Multiple XML files for a BeanFactory?

    Quote Originally Posted by rhasselbaum
    Is there a way to use multiple XML files with a BeanFactory? I know for ApplicationContexts like ClassPathXmlApplicationContext it is easy: just specify a list of files in the constructor. But an app context is too heavyweight for what I'm trying to do.
    To answer your original questions, application contexts are not that heavyweight anyways, but if you really want to use a bean factory instead with multiple xml files, don't use XmlBeanFactory. Just do the variant where you use an XmlBeanDefinitionReader to read in definitions. Do this for each file that you need to read in from. This is shown in the manual in the beans chapter.
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  9. #9

    Default

    Hmm... the only reference I could find to XmlBeanDefinitionReader in the manual was in the DTD (appendix). But I will check it out.

    Thanks.

  10. #10
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    I think there used to be an example in the beans chapter, but then it switched to using XmlBeanFactory. Here's some sample code, it's easy:

    Code:
    ClassPathResource res = new ClassPathResource&#40;"beans.xml"&#41;;
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory&#40;&#41;;
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader&#40;factory&#41;;
    reader.loadBeanDefinitions&#40;res&#41;;
    Just call loadBeanDefinition multipe times, once for each resource. Don't forget to manually call preInstantiateSingletons, if you need that, as bean factories don't do that by themselves.
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

Similar Threads

  1. Replies: 6
    Last Post: Sep 1st, 2005, 09:18 AM
  2. Replies: 2
    Last Post: Jul 29th, 2005, 11:52 AM
  3. Replies: 7
    Last Post: Jul 26th, 2005, 02:48 PM
  4. Unexpected behaviour with multiple config files
    By rgitzel in forum Container
    Replies: 7
    Last Post: Mar 8th, 2005, 07:11 PM
  5. Replies: 0
    Last Post: Nov 3rd, 2004, 05:37 AM

Posting Permissions

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