动态加载 spring xml 配置
Load spring xml config dynamically
在 spring 应用程序启动时,我想扫描计算机上的路径,找到 jar 文件并从 xml 配置构建一个 spring 应用程序上下文他们里面的文件。一切都可以将 jar 文件添加到类路径并创建 ApplicationContext。但我无法从新的上下文中找到任何豆子。所有需要的依赖项都可以在计算机上特定路径中的 jar 文件中获得(通过 maven 复制器插件)期望那些在基础 spring 项目中的依赖项(例如 spring 依赖项本身)。
代码是(用 Kotlin 语言):
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
我尝试了许多其他方法来加载 xml,但其中 none 是成功的。例如:
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()
jar 文件中的 xml 如下所示:
<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">
<import resource="classpath*:/xxx-logic-context.xml"/>
<context:annotation-config/>
<context:component-scan base-package="aa.bbb.ccc.server"/>
</beans>
这真的很有趣:当我定义常规 Bean 而不是使用包扫描功能时,我可以通过某种代码获取 bean
@talex 的出色回答指导了我。我通过设置当前 class 加载器来修复它:
val loader = URLClassLoader(arrayOf(entry.toFile().toURL()), Thread.currentThread().contextClassLoader)
Thread.currentThread().contextClassLoader = loader
在 spring 应用程序启动时,我想扫描计算机上的路径,找到 jar 文件并从 xml 配置构建一个 spring 应用程序上下文他们里面的文件。一切都可以将 jar 文件添加到类路径并创建 ApplicationContext。但我无法从新的上下文中找到任何豆子。所有需要的依赖项都可以在计算机上特定路径中的 jar 文件中获得(通过 maven 复制器插件)期望那些在基础 spring 项目中的依赖项(例如 spring 依赖项本身)。 代码是(用 Kotlin 语言):
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
我尝试了许多其他方法来加载 xml,但其中 none 是成功的。例如:
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()
jar 文件中的 xml 如下所示:
<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">
<import resource="classpath*:/xxx-logic-context.xml"/>
<context:annotation-config/>
<context:component-scan base-package="aa.bbb.ccc.server"/>
</beans>
这真的很有趣:当我定义常规 Bean 而不是使用包扫描功能时,我可以通过某种代码获取 bean
@talex 的出色回答指导了我。我通过设置当前 class 加载器来修复它:
val loader = URLClassLoader(arrayOf(entry.toFile().toURL()), Thread.currentThread().contextClassLoader)
Thread.currentThread().contextClassLoader = loader