Announcement Announcement Module
Collapse
No announcement yet.
Reading xml beans files from jar files problem !!! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reading xml beans files from jar files problem !!!

    Hi,

    I have lots of scattered springs bean xmls on my directory tree.
    So to manage all of them, i read them by dom4j and join them all in one big xml document of type org.dom4j.Document.

    Next, i want to make a XMLBeanFactory and read in this big document.

    so heres my code:

    Code:
    Document myDocument = myXmlDocFactory.getJoinedDocument();
    DocumentSource ds = new DocumentSource(myDocument);
    and here start my problems...
    i dont know how to convert DocumentSource to a Resource

    Code:
    Resource resource = new InputStreamResource(???);
    XmlBeanFactory factory = new XmlBeanFactory(resource);
    So my question is:

    How can i convert a org.dom4j.Document object to Resource type object to force a XmlBeanFactory read my Beans ???

    maybe DocumentSource is not a good class im using ???

    please help me if you know the answer

    thx

    Peter, POLAND

  • #2
    That DOM4j class is a type of SAX source, so it will not work. According to the JavaDoc for the factory, you would need a DOM, like DOM4j's DOMDocument?

    But, why even take this approach? Spring supports the "import" element, and context loaders can take list of files. There is even a way to load multiple files from a path.

    Comment


    • #3
      man...didnt know that.
      can you tell me more ???
      i mean give me some hints and links i could read more.

      all what i want to do is just load xml beans which all are in some root directory. so i just want only pass a context param where are my beans and spring should find all of matching xml beans into this dir and load them.

      phew, i thought that i have to do all this stuff manually...

      thanks in advance.

      Pete

      Comment


      • #4
        You can use, for example, "FileSystemXmlApplicationContext" and either give it the list of files or use wildcards. See http://static.springframework.org/sp...onContext.html

        Like:
        Code:
        ApplicationContext ctx =
            new FileSystemXmlApplicationContext("file:/conf/context.xml");
        or
        
        ApplicationContext ctx =
            new FileSystemXmlApplicationContext("file:/conf/*-context.xml");
        --- jbetancourt

        Comment


        • #5
          ok, but can i read bean definition from jar file ???
          im currently doing pluggable modules which are configured via spring beans and packaged in jar files.

          so i want to look through my all jar files i.e. xxxModule.jar where xxx is arbitrary name and look up a bean xml to process by spring container

          thx in advance

          Pete.

          Comment


          • #6
            What i want to do is to start web application and read in all spring beans placed in jars.
            one thing ive done for now is servletcontextlistener which can read in only one bean xml which unfortunelly is not in jar.
            i just used simple web.xml notation:

            Code:
            	<context-param>
            		<param-name>contextConfigLocation</param-name>
            		<param-value>
            			/WEB-INF/xml/ors/ORSCore/ORSApplicationContext.xml
            		</param-value>
            	</context-param>
            
            	<listener>
            		<listener-class>
            			com.orsframework.core.server.ORSServletListener
            		</listener-class>
            	</listener>
            and implementation:

            Code:
            public class ORSServletListener implements ServletContextListener &#123;
            
            	public void contextDestroyed&#40;ServletContextEvent evt&#41; &#123;
            
            		WebApplicationContext ctx = WebApplicationContextUtils
            				.getWebApplicationContext&#40;evt.getServletContext&#40;&#41;&#41;;
            		ORSModulesManager mgr = &#40;ORSModulesManager&#41; ctx
            				.getBean&#40;"mgr&#58;modulesManager"&#41;;
            		System.out.println&#40;"TEST&#58; "
            				+ mgr.getModulesLoaderEngine&#40;&#41;.getModulesLoaderEngineContext&#40;&#41;
            						.getComponentsLibraryMappingPaths&#40;&#41;.get&#40;0&#41;.toString&#40;&#41;
            						.trim&#40;&#41;&#41;;
            	&#125;
            
            	public void contextInitialized&#40;ServletContextEvent evt&#41; &#123;
            
            	&#125;
            
            &#125;
            how can i initialize my webapp and read all xmls named in some convention placed in jars ???
            im a bit confused if it is possible...

            greets

            pete

            Comment


            • #7
              Pete,

              Just use the 'classpath:' prefix on your XML file locations, to force these to be considered as ClassPathResources, and have them loaded from the classpath. This will allow you to combine multiple XML fragments from multiple XML files.

              Code:
              <context-param>
                    <param-name>contextConfigLocation</param-name>
                <param-value>
                  classapth&#58;some/class/path/myContext.xml
                  classpath&#58;another/class/path/myOtherContext.xml
              ...
                </param-value>
              </context-param>

              Comment


              • #8
                Great

                But what if my xml beans xmls are in several jars ???
                for example there are three jars under WEB-INF/lib:
                smsModule.jar
                mailModule.jar
                graph2DModule.jar

                each one jar has a appCtx.xml with spring beans inside.

                how can i read all these three xmls from those jars and create one ApplicationContext object from above xmls ???

                hmmmm
                hmmmmmm...

                cant sleep till got the solution

                Comment


                • #9
                  You can combine XML fragments of the same name from _all_ classpath locations, by using the form

                  classpath*:appCtx.xml

                  The above example assumes there is no package prefix on the XML file in the jar.

                  Essentially the above results in a ClassLoader call of the form:
                  cl.getResources(path);

                  So it is really the classloader that is finding all instances. But this will find all matching resources of the same name.

                  Comment


                  • #10
                    ok i works quike good.

                    mhmmm but i stiil have problem becuse my xmls within jar files can have different names.

                    for example under myJar.jar there is configBeans.xml
                    and under otherJar there is /config/configurationBeans.xml

                    so one problem is that they have different names and the second
                    they are in different packages under jar files.

                    any suggestions ?

                    Comment


                    • #11
                      Just use multiple classpath:xxxx and classpath*:xxx to refer to all the xml file. You can have multiple location strings combined, as shown in my second-last post.

                      Comment

                      Working...
                      X