`

spring 控制反转与依赖注入原理-学习笔记

阅读更多

 

在Spring中有两个非常重要的概念,控制反转和依赖注入;控制反转将依赖对象的创建和管理交由Spring容器,而依赖注入则是在控制反转的基础上将Spring容器管理的依赖对象注入到应用之中;

所谓依赖注入:在运行期,由外部容器动态将依赖对象注入到组件中。

XML文件解析 + Java反射技术;

首先是XML文件的解析(dom4j),Spring框架对于配置文件的选择是XML文件,根据Spring的规范,配置文件的命名是没有特殊要求的,只是在文件的放置位置上有两种选择;类路径下或者操作系统文件目录下(大多数情况是放到类路径下)。

对于Spring的控制反转和依赖注入来说,唯一使用的是配置文件中的<bean>标签,通过这个标签,Spring就完成了对象的创建和依赖对象的注入工作;

1、首先对于配置文件中的<bean>节点,在Spring框架中存在一个对用的定义接口,叫做BeanDefinition;子啊个类定义了获得<bean>节点中出现的所有属性的方法,例如classNam、scope、factory-method、lazy-init 等等属性;

2、对于<bean>节点的子节点property则完成了属性注入的功能;属性注入有三种方式,构造器注入、属性setter方法注入和注解方式注入;

3、如果是setter方法注入,对于类属性XML配置文件中有两种方法,一是使用property节点的ref属性,一是使用property几点的子节点bean进行内部bean配置;如果是对于基本数据类型进行配置,那么要是用property节点的value属性;


定义自己的关于bean节点、property节点的pojo类文件;

使用注入DOM4J等开源包讲配置文件解析读入;

使用Java的反射技术讲配置文件中的信息setter到我们需要的属性中去;common-beanutils.jar

 

 

	<context:component-scan base-package="com.sample"/>
	<bean id="personService" class="com.spring.junit.test.impl.PersonServiceImpl"></bean>
	<bean id="stockService" class="com.spring.junit.test.impl.StockServiceImpl"></bean>
	
	<bean id="personServiceFactory" class="com.spring.junit.test.impl.PersonServiceBeanFactory" factory-method="createPersonServiceBeanFactory"></bean>
	
	<bean id="personServiceFactory2" class="com.spring.junit.test.impl.PersonServiceBeanFactory"></bean>
	<bean id="stockServiceFactory" factory-bean="personServiceFactory2" factory-method="createStockServiceBeanFactory"></bean>
	
	<bean id="randomBean" class="com.spring.junit.bean.StaticFactoryBean" factory-method="createRandom" scope="prototype"></bean>

	<!-- 集合类型的注入 -->

	通过setter方法注入
	<bean id="user" class="com.sample.bean.User"></bean>
	<bean id="personDao" class="com.sample.dao.impl.PersonDaoBeanImpl"></bean>
	<bean id="personService" class="com.sample.service.impl.PersonServiceBeanImpl">
	   
	   <property name="personDao" ref="personDao"></property>
	   <property name="name" value="jackjson_xu_test"></property>
	   <property name="id" value="108"></property>
	   <property name="sets">
	       <set>
	           <value>第一个</value>
	           <value>第二个</value>
	           <value>第三个</value>
	       </set>
	   </property>
	   <property name="lists">
	       <list>
	           <value>第一個list元素</value>
	           <value>第二個list元素</value>
	           <value>第三個list元素</value>
	           
	       </list>
	   </property>
	   <property name="properties">
	       <props>
	           <prop key="key1">value1</prop>
	           <prop key="key2">value2</prop>
	           <prop key="key3">value3</prop>
	           
	       </props>
	   </property>
	   <property name="maps">
	       <map>
	           <entry key="key-1" value="value-1"></entry>
	           <entry key="key-2" value="value-2"></entry>
	           <entry key="key-3" value="value-3"></entry>
	           <entry key="key-4" value="value-4"></entry>
	       </map>
	   </property>
	   <property name="users">
	       <map>
	           <entry key="U_1001">
	               <ref bean="user"/>
	           </entry>
	           <entry key="U_1002">
                   <ref bean="user"/>
               </entry>
	       </map>
	   </property>
	</bean>
	<!-- 采用内部bean的方式注入 -->

	<bean id="personService" class="com.sample.service.impl.PersonServiceBeanImpl">
	   <property name="personDao">
	       <bean class="com.sample.dao.impl.PersonDaoBeanImpl"/>
	   </property>
	   <property name="name" value="jackjson_xu_test"></property>
	   <property name="id" value="100"></property>
	</bean>
	
	
	<!-- 构造器注入方式 -->
	
	<bean id="personDao" class="com.sample.dao.impl.PersonDaoBeanImpl"></bean>
    <bean id="personService2" class="com.sample.service.impl.PersonServiceBeanImpl2" autowire="byType">
        <constructor-arg index="0" type="com.sample.dao.IPersonDao" ref="personDao"></constructor-arg>
        <constructor-arg index="1" type="java.lang.String" value="http://www.woyo.com"></constructor-arg>
    </bean>
  

 package com.sample.junit;

import java.util.ArrayList;
import java.util.List;
/**
 * Spring xml 属性的方法
 * @author DY
 *
 */
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 com.sample.junit;

/**
 * Spring xml bean 子节点property属性方法
 * 
 * @author DY
 * 
 */
public class PropertyDefinition {
	private String name;
	private String ref;
	private String value;

	public PropertyDefinition(String name, String ref, String value) {
		this.name = name;
		this.ref = ref;
		this.value = 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;
	}

}

 package com.sample.junit;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
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.apache.commons.beanutils.ConvertUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
/**
 * spring装配applicationContext.xml文件
 * @author DY
 *
 */
public class SampleClassPathXMLApplicationContext {
	private Logger logger = Logger.getLogger(SampleClassPathXMLApplicationContext.class);
	private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();
	private Map<String, Object> sigletons = new HashMap<String, Object>();

	public SampleClassPathXMLApplicationContext(String filename) {
		this.readXML(filename);
		this.instanceBeans();	//bean的实例化 Class.forName().newInstance()
		this.annotationInject();//注解
		this.injectObject();	//bean对象的属性注入值
	}
	/**
	 * 注解处理器
	 * 如果注解SampleResouce配置了name属性,则根据name所指定的名称获取要注入的实例引用
	 * 如果注解SampleResouce没有配置name属性,则根据属性所属类型来扫描配置文件获取要注入的实例引用
	 */
	private void annotationInject() {
		for (String beanName : sigletons.keySet()) {
			Object bean = sigletons.get(beanName);
			if (bean != null) {
				this.propertyAnnotation(bean);
				this.fieldAnnotation(bean);
			}
		}
	}
	/**
	 * 处理在所有set方法加入的注解
	 * @param bean 处理的bean对象
	 */
	private void propertyAnnotation(Object bean) {
		try {
			//获取其属性的描述
			PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
			for (PropertyDescriptor properdesc : ps) {
				//获取属性的setter方法
				Method setter = properdesc.getWriteMethod();
				//setter方法上是否存在注解
				if (setter != null && setter.isAnnotationPresent(SampleResource.class)) {
					//获取当前注解,判断name属性是否为空
					SampleResource resouce = setter.getAnnotation(SampleResource.class);
					Object value = null;
					if (resouce.name() != null && !"".equals(resouce.name())) {
						value = sigletons.get(resouce.name());
						setter.setAccessible(true);
						setter.invoke(bean, value);//把引用对象注入到属性
					} else {//如果当前属性没有指定name,则根据类型匹配
						value = sigletons.get(resouce.name());
						if (value == null) {
							for (String key : sigletons.keySet()) {
								//判断当前属性所属类型是否在配置文件中存在
								if (properdesc.getPropertyType().isAssignableFrom(sigletons.get(key).getClass())) {
									value = sigletons.get(key);	//获取类型匹配的实例对象
								}
							}
						}
						//允许访问private方法
						setter.setAccessible(true);
						//把引用对象注入属性
						setter.invoke(bean, value);
					}
				}
			}
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage());
		}
	}
	/**
	 * 处理在字段上的注解
	 * @param bean
	 */
	private void fieldAnnotation (Object bean) {
		try {
			//获取全部属性对象数组
			Field[] fields = bean.getClass().getFields();
			for (Field field : fields) {
				if (field.isAnnotationPresent(SampleResource.class)) {
					SampleResource resouce = field.getAnnotation(SampleResource.class);
					Object value = null;
					if (resouce.name() != null && !"".equals(resouce.name())) {
						value = sigletons.get(resouce.name());
					} else {
						value = sigletons.get(field.getName());
						if (value == null) {
							for (String key : sigletons.keySet()) {
								//根据字段类型匹配
								if (field.getType().isAssignableFrom(sigletons.get(key).getClass())) {
									value = sigletons.get(key);
									break;
								}
							}
						}
					}
					field.setAccessible(true);
					field.set(bean, value);
				}
			}
		} catch (Exception e) {
			e.getLocalizedMessage();
			logger.error("字段注解解析异常:" + e.getLocalizedMessage());
		}
	}
	/**
	 * 为bean对象的属性注入值
	 */
	private void injectObject() {
		for (BeanDefinition 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 value = null;
									if (propertyDefinition.getRef() != null && !"".equals(propertyDefinition.getRef().trim())) {
										value = sigletons.get(propertyDefinition.getRef());
									} else {
										value = ConvertUtils.convert(propertyDefinition.getValue(), properdesc.getPropertyType());
									}
									setter.setAccessible(true);//私有方法给与访问权限
									setter.invoke(bean, value);// 把引用对象注入到属性
								}
								break;
							}
						}
					}
				} catch (Exception e) {
				}
			}
		}
	}

	/**
	 * 完成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<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);
				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");
					String propertyValue = property.attributeValue("value");
					PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName, propertyref, propertyValue);
					beanDefine.getPropertys().add(propertyDefinition);
					System.out.println("propertyName:" + propertyName + "|propertyref:" + propertyref + "|propertyValue:" + propertyValue);
				}
				beanDefines.add(beanDefine);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取bean实例
	 * 
	 * @param beanName
	 * @return
	 */
	public Object getBean(String beanName) {
		return this.sigletons.get(beanName);
	}
}

 package com.sample.junit;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sample.service.IPersonService;

public class SpringTest {

	static ApplicationContext ctx = null;
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		ctx = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
	}
	
	
	@Test public void instanceSpring(){
		IPersonService personService = (IPersonService)ctx.getBean("personService");
		System.out.println(personService);
		personService.save();
	}
	

}
分享到:
评论
2 楼 u011028234 2016-12-02  
楼主你这例子里边的SampleResource实体没有额?
1 楼 yueerba 2016-11-16  
[flash=200,200][url][url][img][list]
[*]
引用
[u][i][b][/b][/i][/u]
[/list][/img][/url][/url][/flash]
|

相关推荐

    Spring.NET学习笔记-控制反转

    控制反转(Inversion of Control,英文缩写为IoC),也叫依赖注入(Dependency Injection)。我个人认为控制反转的意思是依赖对象(控制权)发生转变,由最初的类本身来管理依赖对象转变为IoC框架来管理这些对象,使得...

    javaSpring学习笔记

    在“Java Spring学习笔记”中,你将找到对Spring框架的全面介绍,包括IoC(控制反转)和DI(依赖注入)原理、AOP(面向切面编程)、Spring MVC、Spring Boot等核心内容。每个主题都结合了理论知识和实际示例,帮助你...

    我的Pro Spring 学习笔记 之二 控制反转(IoC)和依赖注入(DI), Spring初步

    源代码 博文链接:https://danielkwo.iteye.com/blog/40945

    spring课堂笔记.docx

    核心概念:深入探讨了 Spring 的核心概念,如控制反转(IoC)和面向切面编程(AOP),以及它们如何改善应用程序的设计和结构。 Spring 容器:介绍了 Spring 容器的不同类型,包括 BeanFactory 和 ...

    Spring.NET学习笔记

    基础篇包括:控制反转,环境搭建,实现简易IOC框架,对象的创建,容器中对象作用域,依赖注入,依赖对象的注入,集合类型的注入等 所有代码都是基于.net平台的C#语言实现的,资源整理自互联网,欢迎爱好学习研究框架...

    spring项目开发学习笔记

    依赖注入DI/IoC控制反转:提供松耦合的一种实现技术 AOP面向切面编程:(可以在不修改源代码的前提下,增强权限,日志,事务功能)切面编程将业务逻辑从应用服务中分离 容器(spring):管理dao,service,action,...包含并...

    spring_ioc.docx

    web开发技术spring框架控制反转思想IOC学习笔记,并对java的静态代理和动态代理进行了相关叙述

    asp.net知识库

    体验.net2.0的优雅(四):Provider、策略、控制反转和依赖注入 泛型最佳实践 asp.net 2.0下嵌套masterpage页的可视化编辑 C# 2.0与泛型 动态调用对象的属性和方法——性能和灵活性兼备的方法 泛型技巧系列:用泛型...

Global site tag (gtag.js) - Google Analytics