spring深入源码1 简单实现ioc机制

用spring也有两年多了 最近一段时间一直在看框架的源代码 从连接池,tomcat到spring 从中学到最多的是代 模式,java反射,设计思想。

 

我们不但要知其然,还要知其所以然。“知其所以然”的最好 办法就是下载源代码,仔细研读,揣摩并领会源代 码的精义,看看这些经过诸多高手修改的源代码究竟藏有什么玄机,我们能从其中学习到哪些设计思想及设计模式,代码架构如何等,我们从源代码中学习的东西太多了。

 

下面我根据spring源码 简单实现自己的依赖注入  通过xml形式配置   在对象中获取xml文件 获取定义好的bean 从而对bean对应的class 实现实例化   使用接口形式

 

接口

 

public interface PersonDao {

	public void add();

}
 

实现类

package cn.leam.dao.impl;

import cn.leam.dao.PersonDao;

public class PersonDaoBean implements PersonDao {
	public void add(){
		System.out.println("执行add()方法");
	}
}
 

服务接口

public interface PersonService {

	public void save();

}

 

服务实现类

public class PersonServiceBean implements PersonService {
	private PersonDao personDao;
	
	public PersonDao getPersonDao() {
		return personDao;
	}

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}
	
	public void save(){
		personDao.add();
	}
}
 

 

首先配置beans.xml    配置DAO,SERVICE实现类

 

<?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.leam.dao.impl.PersonDaoBean"></bean>
          <bean id="personService" class="cn.leam.service.impl.PersonServiceBean">
          	<property name="personDao" ref="personDao"></property>
          </bean>
</beans>
 

 

 

下面模拟spring对xml配置的类进行实例化

 

存放属性的对象

public class prosDefinition {
	private String name;
	private String ref;
	
	public ProsDefinition(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;
	}
	
}
 

 

存放bean的 对象

public class Definition {
	private String id;
	private String className;
	private List<ProsDefinition> propertys = new ArrayList<ProsDefinition>();
	
	public Definition(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;
	}
	
}
 

 

这里是关键点  所有代码都在这里    使用dom4j 解析xml文件中的bean  并获取id和class  再判断元素中是否有引用元素对其一并获取出来存放才Map中 利用java反射一个一个进行实例化

/**
 * 学习版容器
 *
 */
public class LeamClassPathXMLApplicationContext {
	private List<Definition> beanDefines = new ArrayList<Definition>();
	private Map<String, Object> sigletons = new HashMap<String, Object>();
	
	public LeamClassPathXMLApplicationContext(String filename){
		this.readXML(filename);
		this.instanceBeans();
		this.injectObject();
	}
	/**
	 * 为bean对象的属性注入值
	 */
	private void injectObject() {
		for(Definition beanDefinition : beanDefines){
			Object bean = sigletons.get(beanDefinition.getId());
			if(bean!=null){
				try {
					PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass())
                                                      .getPropertyDescriptors();
					for(ProsDefinition 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.invoke(bean, value);//把引用对象注入到属性
								}
								break;
							}
						}
					}
				} catch (Exception e) {
				}
			}
		}
	}
	/**
	 * 完成bean的实例化
	 */
	private void instanceBeans() {
		for(Definition 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<String,String> nsMap = new HashMap<String,String>();
	         nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空间
                 //创建beans/bean查询路径      
                XPath xsub = document.createXPath("//ns:beans/ns: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属性值        
	            Definition beanDefine = new Definition(id, clazz);
	            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");
	            	ProsDefinition propertyDefinition = 
                         new ProsDefinition(propertyName, propertyref);
	            	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);
	}
}

 

上面简单的依赖注入 基本完成 当然spring的源码会管家复杂  我们主要是理解其思想  下面我们来测试

 

public class SpringTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test public void instanceSpring(){
		LeamClassPathXMLApplicationContext ctx = new
                                      LeamClassPathXMLApplicationContext("beans.xml");
		PersonService personService = (PersonService)ctx.getBean("personService");
		personService.save();		
	}
}

 

 

 

最终输出 说明实例化成功

 

执行add()方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值