首页  

spring xml bean 配置读取     所属分类 spring 浏览量 1458
BeanDefinitionReader
AbstractBeanDefinitionReader
XmlBeanDefinitionReader
PropertiesBeanDefinitionReader
GroovyBeanDefinitionReader

XmlBeanDefinitionReader  从 xml配置文件中读取bean配置



import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;


DefaultResourceLoader loader = new DefaultResourceLoader();
Resource resource = loader.getResource("classpath:beans.xml");
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
BeanDefinitionReader reader = new XmlBeanDefinitionReader(registry);
int count = reader.loadBeanDefinitions(resource);
String[] beanDefinitionNames = reader.getRegistry().getBeanDefinitionNames();	
		
BeanDefinition bd = null;
List pvlist = null;
for(String beanName:beanDefinitionNames){
	bd = registry.getBeanDefinition(beanName);
	bd.getPropertyValues().add("name3", "name3-value");		
	pvlist = bd.getPropertyValues().getPropertyValueList();					
}

beans.xml


	
	<bean id="user" class="beandef.User">   
        <property name="name" value="tiger"/> 
        <property name="name2" value="${user.name2}"/>  
    </bean>
    


public class User{	
	private String name;
	private String name2;
	...
}
		
		
{converted=false, name=name, optional=false, value=TypedStringValue: value [tiger], target type [null]}
{converted=false, name=name2, optional=false, value=TypedStringValue: value [${user.name2}], target type [null]}
{converted=false, name=name3, optional=false, value=name3-value}]

上一篇     下一篇
spring和springboot发展历史

spring扩展点

spring bean生命周期及扩展点

为何要定投指数基金

spring5事件机制

红黑树要点整理