- 第一种方式:
CODE:
java
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(Properties.class, ()->props);
AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
// 动态生成 extractConfig 对象
StaticApplicationContext configContext = new StaticApplicationContext();
configContext.registerBeanDefinition("params", beanDefinition);
configContext.refresh();
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
configLocations, configContext);
XML:
xml
<!-- 参数注入方式 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="params" />
</bean>
- 另一种方式:
java
Properties prop = ...;
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setProperties(prop);
this.context = new XmlBeanFactory(new ClassPathResource(runtimeContextFile));
ppc.postProcessBeanFactory(this.context);