Announcement Announcement Module
Collapse
No announcement yet.
Multiple XML files for a BeanFactory? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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?

    Comment


    • #3
      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.)

      Comment


      • #4
        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.

        Comment


        • #5
          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

          Comment


          • #6
            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...

            Comment


            • #7
              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.

              Comment


              • #8
                Re: Multiple XML files for a BeanFactory?

                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.

                Comment


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

                  Thanks.

                  Comment


                  • #10
                    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.

                    Comment

                    Working...
                    X