IOC,直观地讲,就是容器控制程序之间的关系,而非传统实现中,由程序代码直接操控。这也就是所谓“控制反转”的概念所在。控制权由应用代码中转到了外部容器,控制权的转移是所谓反转。IoC还有另外一个名字——“依赖注入(Dependency Injection)”。从名字上理解,所谓依赖注入,即组件之间的依赖关系由容器在运行期决定,形象地说,即由容器动态地将某种依赖关系注入到组件之中。
下面我根据spring源码 简单实现自己的依赖注入 通过xml形式配置 在对象中获取xml文件 获取定义好的bean 从而对bean对应的class 实现实例化 使用接口形式
接口
- public interface PersonDao {
- public void add();
- }
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()方法");
- }
- }
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 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();
- }
- }
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>
<?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;
- }
- }
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;
- }
- }
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属性值
- 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);
- }
- }
/**
* 学习版容器
*
*/
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的源码会管家复杂 我们主要是理解其思想 下面我们来测试
- 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();
- }
- }
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()方法