title: Load spring xml config dynamically
body: <p>At the startup time of a spring app, I want to scan a path on the computer, find the jar files and build a spring application context from an xml config files inside them. Every thing is OK to add jar file to the classpath and making an ApplicationContext. But I can't find any beans from new context. All of needed dependencies are available in the jar files in the specific path on computer (via a maven copier plugin) expect those dependencies which are in the base spring project (for example spring dependency itself).
  The code is (In Kotlin language):</p>
  
  <pre><code>var loader = URLClassLoader(arrayOf(entry.toFile().toURL()), Thread.currentThread().contextClassLoader)
  ...
  val context = ClassPathXmlApplicationContext("classpath*:/$name")//name is xml file. I'm sure the address in classpath is right. context is not creating when the address in wrong. for example: classpath://$name
  val services = context.getBeanNamesForType(IService::class.java)//services is empty
  </code></pre>
  
  <p>I have tried many other ways to load the xml but none of them was successful. for example:</p>
  
  <pre><code>val beans = DefaultListableBeanFactory(applicationContext)
  val reader = XmlBeanDefinitionReader(beans)
  reader.beanClassLoader = loader
  reader.resourceLoader = resourceLoader
  reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD)
  jarFile.getInputStream(jarEntry).use {
      reader.loadBeanDefinitions(EncodedResource(InputStreamResource(it)))
  }
  beans.preInstantiateSingletons()
  </code></pre>
  
  <p>the xml inside jar file looks like this:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"&gt;
  &lt;import resource="classpath*:/xxx-logic-context.xml"/&gt;
  &lt;context:annotation-config/&gt;
  &lt;context:component-scan base-package="aa.bbb.ccc.server"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>It's really interesting: When I define regular Beans instead using package scaning feature, I can get the bean in a sort of code</p>