ITeye博客

Spring的IOC机制

wenbing2610 · 2012-03-24 22:37 · web应用 · Spring · Web前端 · spring ioc · Markdown

IOC,直观地讲,就是容器控制程序之间的关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在。控制权由应用代码中转到了外部容器,控制权的转移是所谓反转。IoC还有另外一个名字——“依赖注入(Dependency Injection)”。从名字上理解,所谓依赖注入,即组件之间的依赖关系由容器在运行期决定,形象地说,即由容器动态地将某种依赖关系注入到组件之中。 

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

 

接口

 

Java代码 复制代码 收藏代码

  1. public interface PersonDao {
  2. public void add();
  3. }
public interface PersonDao {

	public void add();

}

 

实现类

Java代码 复制代码 收藏代码

  1. package cn.leam.dao.impl;
  2. import cn.leam.dao.PersonDao;
  3. public class PersonDaoBean implements PersonDao {
  4. public void add(){
  5. System.out.println("执行add()方法");
  6. }
  7. }
package cn.leam.dao.impl;

import cn.leam.dao.PersonDao;

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

 

服务接口

Java代码 复制代码 收藏代码

  1. public interface PersonService {
  2. public void save();
  3. }
public interface PersonService {

	public void save();

}

 

服务实现类

Java代码 复制代码 收藏代码

  1. public class PersonServiceBean implements PersonService {
  2. private PersonDao personDao;
  3. public PersonDao getPersonDao() {
  4. return personDao;
  5. }
  6. public void setPersonDao(PersonDao personDao) {
  7. this.personDao = personDao;
  8. }
  9. public void save(){
  10. personDao.add();
  11. }
  12. }
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代码 复制代码 收藏代码

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
  6. <bean id="personDao" class="cn.leam.dao.impl.PersonDaoBean"></bean>
  7. <bean id="personService" class="cn.leam.service.impl.PersonServiceBean">
  8. <property name="personDao" ref="personDao"></property>
  9. </bean>
  10. </beans>
<?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配置的类进行实例化

 

存放属性的对象

Java代码 复制代码 收藏代码

  1. public class prosDefinition {
  2. private String name;
  3. private String ref;
  4. public ProsDefinition(String name, String ref) {
  5. this.name = name;
  6. this.ref = ref;
  7. }
  8. public String getName() {
  9. return name;
  10. }
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14. public String getRef() {
  15. return ref;
  16. }
  17. public void setRef(String ref) {
  18. this.ref = ref;
  19. }
  20. }
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的 对象

Java代码 复制代码 收藏代码

  1. public class Definition {
  2. private String id;
  3. private String className;
  4. private List<ProsDefinition> propertys = new ArrayList<ProsDefinition>();
  5. public Definition(String id, String className) {
  6. this.id = id;
  7. this.className = className;
  8. }
  9. public String getId() {
  10. return id;
  11. }
  12. public void setId(String id) {
  13. this.id = id;
  14. }
  15. public String getClassName() {
  16. return className;
  17. }
  18. public void setClassName(String className) {
  19. this.className = className;
  20. }
  21. public List<PropertyDefinition> getPropertys() {
  22. return propertys;
  23. }
  24. public void setPropertys(List<PropertyDefinition> propertys) {
  25. this.propertys = propertys;
  26. }
  27. }
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反射一个一个进行实例化

Java代码 复制代码 收藏代码

  1. /**
  2. * 学习版容器
  3. *
  4. */
  5. public class LeamClassPathXMLApplicationContext {
  6. private List<Definition> beanDefines = new ArrayList<Definition>();
  7. private Map<String, Object> sigletons = new HashMap<String, Object>();
  8. public LeamClassPathXMLApplicationContext(String filename){
  9. this.readXML(filename);
  10. this.instanceBeans();
  11. this.injectObject();
  12. }
  13. /**
  14. * 为bean对象的属性注入值
  15. */
  16. private void injectObject() {
  17. for(Definition beanDefinition : beanDefines){
  18. Object bean = sigletons.get(beanDefinition.getId());
  19. if(bean!=null){
  20. try {
  21. PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass())
  22. .getPropertyDescriptors();
  23. for(ProsDefinition propertyDefinition : beanDefinition.getPropertys()){
  24. for(PropertyDescriptor properdesc : ps){
  25. if(propertyDefinition.getName().equals(properdesc.getName())){
  26. Method setter = properdesc.getWriteMethod();
  27. //获取属性的setter方法 ,private
  28. if(setter!=null){
  29. Object value = sigletons.get(propertyDefinition.getRef());
  30. setter.invoke(bean, value);//把引用对象注入到属性
  31. }
  32. break;
  33. }
  34. }
  35. }
  36. } catch (Exception e) {
  37. }
  38. }
  39. }
  40. }
  41. /**
  42. * 完成bean的实例化
  43. */
  44. private void instanceBeans() {
  45. for(Definition beanDefinition : beanDefines){
  46. try {
  47. if(beanDefinition.getClassName()!=null && !"".
  48. equals(beanDefinition.getClassName().trim()))
  49. sigletons.put(beanDefinition.getId(),
  50. Class.forName(beanDefinition.getClassName()).newInstance());
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56. /**
  57. * 读取xml配置文件
  58. * @param filename
  59. */
  60. private void readXML(String filename) {
  61. SAXReader saxReader = new SAXReader();
  62. Document document=null;
  63. try{
  64. URL xmlpath = this.getClass().getClassLoader().getResource(filename);
  65. document = saxReader.read(xmlpath);
  66. Map<String,String> nsMap = new HashMap<String,String>();
  67. nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空间
  68. //创建beans/bean查询路径
  69. XPath xsub = document.createXPath("//ns:beans/ns:bean");
  70. xsub.setNamespaceURIs(nsMap);//设置命名空间
  71. List<Element> beans = xsub.selectNodes(document);//获取文档下所有bean节点
  72. for(Element element: beans){
  73. String id = element.attributeValue("id");//获取id属性值
  74. String clazz = element.attributeValue("class"); //获取class属性值
  75. BeanDefinition beanDefine = new BeanDefinition(id, clazz);
  76. XPath propertysub =  element.createXPath("ns:property");
  77. propertysub.setNamespaceURIs(nsMap);//设置命名空间
  78. List<Element> propertys = propertysub.selectNodes(element);
  79. for(Element property : propertys){
  80. //元素内部引用的属性也获取
  81. String propertyName = property.attributeValue("name");
  82. String propertyref = property.attributeValue("ref");
  83. ProsDefinition propertyDefinition =
  84. new ProsDefinition(propertyName, propertyref);
  85. beanDefine.getPropertys().add(propertyDefinition);
  86. }
  87. beanDefines.add(beanDefine);
  88. }
  89. }catch(Exception e){
  90. e.printStackTrace();
  91. }
  92. }
  93. /**
  94. * 获取bean实例
  95. * @param beanName
  96. * @return
  97. */
  98. public Object getBean(String beanName){
  99. return this.sigletons.get(beanName);
  100. }
  101. }
/**
 * 学习版容器
 *
 */
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属性值        
	            BeanDefinition beanDefine = new BeanDefinition(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的源码会管家复杂  我们主要是理解其思想  下面我们来测试

 

Java代码 复制代码 收藏代码

  1. public class SpringTest {
  2. @BeforeClass
  3. public static void setUpBeforeClass() throws Exception {
  4. }
  5. @Test public void instanceSpring(){
  6. LeamClassPathXMLApplicationContext ctx = new
  7. LeamClassPathXMLApplicationContext("beans.xml");
  8. PersonService personService = (PersonService)ctx.getBean("personService");
  9. personService.save();
  10. }
  11. }
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)

暂无评论。

评论功能维护中,当前仅展示历史评论。