深入探索spring技术内幕(四): 剖析@Resource注解实现原理与注解注入(二)

2014-11-23 21:44:00 · 作者: · 浏览: 22
on beanDefinition : beanDefines) { Object bean = sigletons.get(beanDefinition.getId()); if (bean != null) { try { PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); for (PropertyDefinition propertyDefinition : beanDefinition.getPropertys()) { for (PropertyDescriptor properdesc : ps) { if (propertyDefinition.getName().equals(properdesc.getName())) { Method setter = properdesc.getWriteMethod(); // 获取属性的setter方法 if (setter != null) { Object injectBean = null; if (propertyDefinition.getRef() != null && !"".equals(propertyDefinition.getRef().trim())) { injectBean = sigletons.get(propertyDefinition.getRef()); } else { injectBean = ConvertUtils.convert(propertyDefinition.getValue(), properdesc.getPropertyType()); } setter.setAccessible(true); // private method setter.invoke(bean, injectBean); // 把引用对象注入到属性 } break; } } } } catch (Exception e) { e.printStackTrace(); } } } } /** * 完成bean的实例化 */ private void instanceBeans() { for (BeanDefinition beanDefinition : beanDefines) { try { if (beanDefinition.getClassName() != null && !"".equals(beanDefinition.getClassName().trim())) sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance()); } catch (Exception e) { e.printStackTrace(); } } } /** * 读取xml配置文件 * * @param filename */ 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 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 beans = xsub.selectNodes(document);// 获取文档下所有bean节点 for (Element element : beans) { String id = element.attributeva lue("id");// 获取id属性值 String clazz = element.attributeva lue("class"); // 获取class属性值 BeanDefinition beanDefine = new BeanDefinition(id, clazz); XPath propertysub = element.createXPath("ns:property"); propertysub.setNamespaceURIs(nsMap);// 设置命名空间 List propertys = propertysub.selectNodes(element); for (Element property : propertys) { String propertyName = property.attributeva lue("name"); String propertyRef = property.attributeva lue("ref"); String propertyValue = property.attributeva lue("value"); PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName, propertyRef, propertyValue); beanDefine.getPropertys().add(propertyDefinition); } beanDefines.add(beanDefine); } } catch (Exception e) { e.printStackTrace(); } } /** * 获取bean实例 * * @param beanName * @return */ public Object getBean(String beanName) { return this.sigletons.get(beanName); } }

③ beans.xml

  

  
	
   
	
   
	
   

  
④ PersonServiceImpl

import com.zdp.dao.PersonDao;
import com.zdp.myspring.MyResource;
import com.zdp.service.PersonService;

public class PersonServiceImpl implements PersonService {
	private PersonDao personDao;
	
	@MyResource(name="personDao") 
	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public void save() {
		personDao.save();
	}
}
⑤ 测试一下

import org.junit.Test;
import com.zdp.service.PersonService;
import com.zdp.myspring.ClassPathXMLApplicationContext;
public cla