自定义Spring实现bean属性值的注入
引入commons-beanutils.jar文件。映射String类型到对应的数据类型
public class PropertyDefinition {
private String name;
private String ref;
private String value;
public PropertyDefinition() {
}
public PropertyDefinition(String name, String ref,String value) {
this.name = name;
this.ref = ref;
this.setValue(value);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this.ref = ref;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public class CnpcClassPathXmlApplicationContext {
private List<BeanDefinition> beanDefines = new ArrayList<>();
private Map<String, Object> singletons = new HashMap<>();
public CnpcClassPathXmlApplicationContext(String fileName) {
this.readXML(fileName);
this.instanceBeans();
this.injectObject();
}
/**
* 注入property属性,构造bean与bean的关联关系
*/
private void injectObject() {
for (BeanDefinition beanDefinition : beanDefines) { // 循环所有的bean
Object bean = singletons.get(beanDefinition.getId());
// com.spring.study.service.impl.UserServiceImpl@47ca5c4f
// 取得实例化后的bean对象
if (bean != null) {
try {
PropertyDescriptor[] ps = Introspector.getBeanInfo(
bean.getClass()).getPropertyDescriptors();
for (PropertyDefinition propertyDefinition : beanDefinition
.getPropertys()) { // 获取bean下面的property
for (PropertyDescriptor properdesc : ps) {
if (propertyDefinition.getName().equals(
properdesc.getName())) {
Method setter = properdesc.getWriteMethod(); // 获取属性的setter方法
if (setter != null) {
Object value = null;
if (propertyDefinition.getRef() != null
&& !"".equals(propertyDefinition
.getRef())) {
value = singletons
.get(propertyDefinition
.getRef());
} else {
value = <strong style="background-color: rgb(102, 255, 153);">ConvertUtils.convert(
propertyDefinition.getValue(),
properdesc.getPropertyType());</strong>
}
setter.setAccessible(true); // private
setter.invoke(bean, value); // 把引用对象注入到属性
}
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 完成bean的实例化
*/
private void instanceBeans() {
for (BeanDefinition beanDefine : beanDefines) {
if (beanDefine != null && !"".equals(beanDefine.getClassName()))
try {
singletons.put(beanDefine.getId(),
Class.forName(beanDefine.getClassName())
.newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 读取xml配置文件
*
* @param fileName
*/
@SuppressWarnings("unchecked")
private void readXML(String fileName) {
SAXReader saxReader = new SAXReader();
Document document = null;
try {
URL xmlpath = this.getClass().getClassLoader()
.getResource(fileName);
document = saxReader.read(xmlpath);
Map<String, String> nsMap = new HashMap<>();
nsMap.put("ns", "http://www.springframework.org/schema/beans");// 加入命名空间
XPath xsub = document.createXPath("//ns:beans/ns:bean");// 创建beans/bean查询路径
xsub.setNamespaceURIs(nsMap);// 设置命名空间
List<Element> beans = xsub.selectNodes(document);// 读取文档下所有bean节点
for (Element element : beans) {
String id = element.attributeValue("id");// 获取id属性值
String clazz = element.attributeValue("class");// 获取class属性值
BeanDefinition beanDefintion = new BeanDefinition(id, clazz);
XPath propertysub = document.createXPath("ns:property");
propertysub.setNamespaceURIs(nsMap);
List<Element> propertys = propertysub.selectNodes(element);
for (Element propertyElement : propertys) {
String name = propertyElement.attributeValue("name");
String ref = propertyElement.attributeValue("ref");
<strong>String value = propertyElement.attributeValue("value");</strong>
// System.out.println(name + "--" + ref);
PropertyDefinition propertyDefintion = new PropertyDefinition(
name, ref, value);
beanDefintion.getPropertys().add(propertyDefintion);
}
beanDefines.add(beanDefintion);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Object getBean(String beanName) {
return singletons.get(beanName);
}
}