spring学习笔记整理--05(编码剖析Spring依赖注入的原理)
今天看了半小时视频,觉得自己的java很差,要恶补了,java核心要翻出来好好看看了,哭!
service中通过spring配置dao属性想必大家已经很熟悉了,但是要是自己写个类来实现,人就有点晕了,今天就贴个代码
,日后慢慢消化!
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="personDao" class="cn.dao.impl.PersonDaoImpl"></bean> <bean id="personService" class="cn.service.impl.PersonServiceImpl"> <property name="personDao" ref="personDao"></property> </bean> </beans> ----------------------------------------------------------------------------------------- package cn.dao.impl; import cn.dao.PersonDao; public class PersonDaoImpl implements PersonDao { public void save(){ System.out.println("This is dao' save()!"); } } ----------------------------------------------------------------------------------------- package cn.dao; public interface PersonDao { public abstract void save(); } ----------------------------------------------------------------------------------------- package cn.service.impl; import cn.dao.PersonDao; import cn.service.PersonService; public class PersonServiceImpl implements PersonService { private PersonDao personDao; public void save(){ System.out.println("This is save()!"); personDao.save(); } public void setPersonDao(PersonDao personDao) { this.personDao = personDao; } public PersonDao getPersonDao() { return personDao; } } ----------------------------------------------------------------------------------------- package cn.service; public interface PersonService { public abstract void save(); } ----------------------------------------------------------------------------------------- package commons.tz; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.XPath; import org.dom4j.io.SAXReader; public class TzClassPathXmlApplicationContext { private List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>(); private Map<String, Object> sigletons = new HashMap<String, Object>(); public TzClassPathXmlApplicationContext(String fileName) { this.readXml(fileName); this.instanceBeans(); this.injectObject(); } /** * 为bean对象的属性注入值 */ private void injectObject() { for(BeanDefinition beanDefinition : beanDefinitions){ 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方法,private if(setter != null){ Object value = sigletons.get(propertyDefinition.getRef()); setter.setAccessible(true);//使private方法可以调用 setter.invoke(bean, value);//把引用对象注入到属性 } break; } } } } catch (Exception e) { e.printStackTrace(); } } } } /* * 实例化一个bean对象 * */ private void instanceBeans() { for (BeanDefinition beanDefinition : beanDefinitions) { if (beanDefinition.getClassName() != null && !"".equals(beanDefinition.getClassName().trim())) { try { sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance()); } catch (Exception e) { e.printStackTrace(); } } } } /* * 读取xml文件 * */ 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<String, String>(); 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 beanDefine = new BeanDefinition(id, clazz); //得到bean对象的所有的属性值,然后注入到bean对象的实例类中 XPath propertySub = element.createXPath("ns:property"); propertySub.setNamespaceURIs(nsMap); List<Element> propertys = propertySub.selectNodes(element); for(Element property : propertys){ String propertyName = property.attributeValue("name"); String propertyRef = property.attributeValue("ref"); PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName,propertyRef); beanDefine.getPropertys().add(propertyDefinition); } beanDefinitions.add(beanDefine); } } catch (Exception e) { e.printStackTrace(); } } public Object getBean(String fileName) { return this.sigletons.get(fileName); } } ----------------------------------------------------------------------------------------- package junit.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import commons.tz.TzClassPathXmlApplicationContext; import cn.service.PersonService; public class Spring02Test { @Test public void instanceSping() { //ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); TzClassPathXmlApplicationContext ctx = new TzClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService"); personService.save(); } } ----------------------------------------------------------------------------------------- package commons.tz; import java.util.ArrayList; import java.util.List; public class BeanDefinition { private String id; private String className; private List<PropertyDefinition> propertys = new ArrayList<PropertyDefinition>(); public BeanDefinition(String id, String className) { this.id = id; this.className = className; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public List<PropertyDefinition> getPropertys() { return propertys; } public void setPropertys(List<PropertyDefinition> propertys) { this.propertys = propertys; } } ----------------------------------------------------------------------------------------- package commons.tz; public class PropertyDefinition { private String name; private String ref; public PropertyDefinition(String name, String ref) { this.name = name; this.ref = ref; } 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; } }