Java spring从使用属性注入的xml动态加载bean
我需要从XML加载bean定义。文件位于由属性配置的远程位置(http)。属性也应该从远程位置(SpringCloudConfig服务器)加载Java spring从使用属性注入的xml动态加载bean,java,xml,spring,applicationcontext,spring-bean,Java,Xml,Spring,Applicationcontext,Spring Bean,我需要从XML加载bean定义。文件位于由属性配置的远程位置(http)。属性也应该从远程位置(SpringCloudConfig服务器)加载 <import resource="${MY_XML_URI}/myBeans.xml"/> 限制条件: <import resource="${MY_XML_URI}/myBeans.xml"/> Spring 4.3.14(未启动) 应处于运行时且在已加载我的属性后 xml中定义的bean引用上下文中的属性 服务器URI
<import resource="${MY_XML_URI}/myBeans.xml"/>
限制条件:
<import resource="${MY_XML_URI}/myBeans.xml"/>
- Spring 4.3.14(未启动)
- 应处于运行时且在已加载我的属性后
- xml中定义的bean引用上下文中的属性
- 服务器URI(从中获取xml)应位于properties中,而不是环境变量或profile depandant中
MY\u XML\u URI
作为环境变量传递时,我的当前设置运行良好:
<import resource="${MY_XML_URI}/myBeans.xml"/>
${SPRING\u CONFIG\u URI}/master/application-${SPRING.profiles.active}.properties
<import resource="${MY_XML_URI}/myBeans.xml"/>
<import resource="${MY_XML_URI}/myBeans.xml"/>
@Configuration
public class RiskConfig {
@Value("${xml.server.uri}")
private String xmlUri;
@Autowired
@Bean
public Object myBean(ApplicationContext applicationContext) {
Resource resource = applicationContext.getResource(xmlUri + "myBeans.xml");
// not working since its not loading the beans to the main context
// GenericApplicationContext genericApplicationContext = new GenericApplicationContext(applicationContext);
// XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(genericApplicationContext);
// reader.loadBeanDefinitions(resource);
AutowireCapableBeanFactory factory = applicationContext.getAutowireCapableBeanFactory();
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry);
reader.loadBeanDefinitions(resource);
return new Object();
}
}
最后—是否有一种方法可以通过编程将bean从xml加载到父应用程序上下文(已经存在),尽管它们被注入了属性。导入元素中的占位符仅从系统属性或环境中解析。不是从加载的属性文件。加载属性文件和上下文不是一个多过程操作,因此您不可能实现所需的操作。如果不自己编写代码,至少是不可能的。@m-deinum我不介意编写一些代码,您有什么具体的建议或一些spring钩子来实现这一点吗?
<import resource="${MY_XML_URI}/myBeans.xml"/>