知识大全 模拟spring框架注入实现原理
Posted 路径
篇首语:行是知之始,知是行之成。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 模拟spring框架注入实现原理相关的知识,希望对你有一定的参考价值。
定义一些抽象的方法
[java]
package huxin springinject dao;
public interface Person
public void save();
public void useAxe();
package huxin springinject dao;
public interface Person
public void save();
public void useAxe();
[java]
package huxin springinject dao;
public interface Axe
public void chop();
package huxin springinject dao;
public interface Axe
public void chop();
实现的一些方法
[java]
package huxin springinject dao impl;
import huxin springinject dao Axe;
import huxin springinject dao Person;
public class Chinese implements Person
private Axe axe;
public Axe getAxe()
return axe;
public void setAxe(Axe axe)
this axe = axe;
public void save()
System out println( 保存人的方法 );
public void useAxe()
axe chop();
package huxin springinject dao impl;
import huxin springinject dao Axe;
import huxin springinject dao Person;
public class Chinese implements Person
private Axe axe;
public Axe getAxe()
return axe;
public void setAxe(Axe axe)
this axe = axe;
public void save()
System out println( 保存人的方法 );
public void useAxe()
axe chop();
[java]
package huxin springinject dao impl;
import huxin springinject dao Axe;
public class StoneAxe implements Axe
@Override
public void chop()
System out println( 铁斧头砍柴真慢 );
package huxin springinject dao impl;
import huxin springinject dao Axe;
public class StoneAxe implements Axe
@Override
public void chop()
System out println( 铁斧头砍柴真慢 );
这里是关键spring框架模拟的实现的一些原理!!!
[java]
package junit test;
import java beans Introspector;
import java beans PropertyDescriptor;
import java lang reflect Method;
import URL;
import java util ArrayList;
import java util HashMap;
import java util List;
import java util Map;
import mons beanutils ConvertUtils;
import dom j Document;
import dom j Element;
import dom j XPath;
import dom j io SAXReader;
public class ApplicationContext
List<BeanInformation> beansInformation = new ArrayList<BeanInformation>();
Map<String Object> singleton = new HashMap<String Object>();
public ApplicationContext();
public ApplicationContext(String filename)
readXml(filename);
initBean();
this injectObject();
public void readXml(String filename)
SAXReader saxReader = new SAXReader();
Document document = null;
try
//使用反射机制 通过文件名加载文件路径
URL xmlPath = this getClass() getClassLoader() getResource(filename);
//通过文件路径获得这个文件的document对象
document = saxReader read(xmlPath);
Map<String String> nsMap = new HashMap<String String>();
nsMap put( ns /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)
System out println(element attributeValue( id ));
System out println(element attributeValue( class ));
BeanInformation beanInformation = new BeanInformation();
beanInformation setId(element attributeValue( id ));
beanInformation setName(element attributeValue( class ));
XPath xref = element createXPath( ns:property );//创建properties查询路径
xref setNamespaceURIs(nsMap);//设置命名空间
List<Element> propertys = xref selectNodes(element);
for(Element property : propertys)
PropertyInformation propertyInformation = new PropertyInformation();
propertyInformation setName(property attributeValue( name ));
propertyInformation setRef(property attributeValue( ref ));
propertyInformation setValue(property attributeValue( value ));
beanInformation getPropertyInformation() add(propertyInformation);
beansInformation add(beanInformation);
catch(Exception e)
e printStackTrace();
public void initBean()
for(BeanInformation beanInformation: beansInformation)
if(beanInformation getName()!=null && ! equals(beanInformation getName()))
//通过反射机制 根据名字初始化这个类
try
singleton put(beanInformation getId() Class forName(beanInformation getName()) newInstance());
catch (Exception e)
e printStackTrace();
/**
* 关于注入的实现
*/
private void injectObject()
for(BeanInformation beanInformation : beansInformation)
Object bean = singleton get(beanInformation getId());
if(bean!=null)
try
PropertyDescriptor[] ps = Introspector getBeanInfo(bean getClass()) getPropertyDescriptors();
for(PropertyInformation propertyInformation : beanInformation getPropertyInformation())
for(PropertyDescriptor properdesc : ps)
if(propertyInformation getName() equals(properdesc getName()))
Method setter = properdesc getWriteMethod();//获取属性的setter方法 private
if(setter!=null)
Object value = null;
if(propertyInformation getRef()!=null && ! equals(propertyInformation getRef() trim()))
value = singleton get(propertyInformation getRef());
else
value = nvert(propertyInformation getValue() properdesc getPropertyType());
setter setAccessible(true);
setter invoke(bean value);//把引用对象注入到属性
break;
catch (Exception e)
public Object getBean(String id)
return this singleton get(id);
package junit test;
import java beans Introspector;
import java beans PropertyDescriptor;
import java lang reflect Method;
import URL;
import java util ArrayList;
import java util HashMap;
import java util List;
import java util Map;
import mons beanutils ConvertUtils;
import dom j Document;
import dom j Element;
import dom j XPath;
import dom j io SAXReader;
public class ApplicationContext
List<BeanInformation> beansInformation = new ArrayList<BeanInformation>();
Map<String Object> singleton = new HashMap<String Object>();
public ApplicationContext();
public ApplicationContext(String filename)
readXml(filename);
initBean();
this injectObject();
public void readXml(String filename)
SAXReader saxReader = new SAXReader();
Document document = null;
try
//使用反射机制 通过文件名加载文件路径
URL xmlPath = this getClass() getClassLoader() getResource(filename);
//通过文件路径获得这个文件的document对象
document = saxReader read(xmlPath);
Map<String String> nsMap = new HashMap<String String>();
nsMap put( ns /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)
System out println(element attributeValue( id ));
System out println(element attributeValue( class ));
BeanInformation beanInformation = new BeanInformation();
beanInformation setId(element attributeValue( id ));
beanInformation setName(element attributeValue( class ));
XPath xref = element createXPath( ns:property );//创建properties查询路径
xref setNamespaceURIs(nsMap);//设置命名空间
List<Element> propertys = xref selectNodes(element);
for(Element property : propertys)
PropertyInformation propertyInformation = new PropertyInformation();
propertyInformation setName(property attributeValue( name ));
propertyInformation setRef(property attributeValue( ref ));
propertyInformation setValue(property attributeValue( value ));
beanInformation getPropertyInformation() add(propertyInformation);
beansInformation add(beanInformation);
catch(Exception e)
e printStackTrace();
public void initBean()
for(BeanInformation beanInformation: beansInformation)
if(beanInformation getName()!=null && ! equals(beanInformation getName()))
//通过反射机制 根据名字初始化这个类
try
singleton put(beanInformation getId() Class forName(beanInformation getName()) newInstance());
catch (Exception e)
e printStackTrace();
/**
* 关于注入的实现
*/
private void injectObject()
for(BeanInformation beanInformation : beansInformation)
Object bean = singleton get(beanInformation getId());
if(bean!=null)
try
PropertyDescriptor[] ps = Introspector getBeanInfo(bean getClass()) getPropertyDescriptors();
for(PropertyInformation propertyInformation : beanInformation getPropertyInformation())
for(PropertyDescriptor properdesc : ps)
if(propertyInformation getName() equals(properdesc getName()))
Method setter = properdesc getWriteMethod();//获取属性的setter方法 private
if(setter!=null)
Object value = null;
if(propertyInformation getRef()!=null && ! equals(propertyInformation getRef() trim()))
value = singleton get(propertyInformation getRef());
else
value = nvert(propertyInformation getValue() properdesc getPropertyType());
setter setAccessible(true);
setter invoke(bean value);//把引用对象注入到属性
break;
catch (Exception e)
public Object getBean(String id)
return this singleton get(id);
[java]
package junit test;
import java util HashSet;
import java util Set;
public class BeanInformation
private String id;
private String name;
private Set<PropertyInformation> propertyInformation = new HashSet<PropertyInformation>();
public String getId()
return id;
public void setId(String id)
this id = id;
public String getName()
return name;
public void setName(String name)
this name = name;
public Set<PropertyInformation> getPropertyInformation()
return propertyInformation;
public void setPropertyInformation(Set<PropertyInformation> propertyInformation)
this propertyInformation = propertyInformation;
package junit test;
import java util HashSet;
import java util Set;
public class BeanInformation
private String id;
private String name;
private Set<PropertyInformation> propertyInformation = new HashSet<PropertyInformation>();
public String getId()
return id;
public void setId(String id)
this id = id;
public String getName()
return name;
public void setName(String name)
this name = name;
public Set<PropertyInformation> getPropertyInformation()
return propertyInformation;
public void setPropertyInformation(Set<PropertyInformation> propertyInformation)
this propertyInformation = propertyInformation;
[java]
package junit test;
public class PropertyInformation
private String name;
private String ref;
private String 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 junit test;
public class PropertyInformation
private String name;
private String ref;
private String 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;
测试类
[java]
package junit test;
import huxin springinject dao Person;
public class Test
public static void main(String[] args)
ApplicationContext ac = new ApplicationContext( applicationContext xml );
Person person = (Person)ac getBean( chinese );
person save();
person useAxe();
package junit test;
import huxin springinject dao Person;
public class Test
public static void main(String[] args)
ApplicationContext ac = new ApplicationContext( applicationContext xml );
Person person = (Person)ac getBean( chinese );
person save();
person useAxe();
[]
<?xml version= encoding= UTF ?>
<beans xmlns= /schema/beans
xmlns:xsi= / /XMLSchema instance
xmlns:context= /schema/context
xsi:schemaLocation= /schema/beans
/schema/beans/spring beans xsd
/schema/context
/schema/context/spring context xsd >
<bean id= chinese class= huxin springinject dao impl Chinese >
<property name= axe ref= stoneAxe />
</bean>
<bean id= stoneAxe class= huxin springinject dao impl StoneAxe />
</beans>
<?xml version= encoding= UTF ?>
<beans xmlns= /schema/beans
xmlns:xsi= / /XMLSchema instance
xmlns:context= /schema/context
xsi:schemaLocation= /schema/beans
/schema/beans/spring beans xsd
/schema/context
/schema/context/spring context xsd >
<bean id= chinese class= huxin springinject dao impl Chinese >
<property name= axe ref= stoneAxe />
</bean>
<bean id= stoneAxe class= huxin springinject dao impl StoneAxe />
</beans>
cha138/Article/program/Java/ky/201311/28763相关参考
Spring框架概述 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! SPRING框架——由来和发
Spring系列第1部分:Spring框架简介(图) 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
Spring中DI设置器注入 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Java的反射机制可
Spring依赖注入的两种方式比对 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 下面对spri
在spring中提供了三种可供选择的注入方式 提供set/get方法 构造函数 工厂方法的运用 大家对这三种方法的选择都是仁者见仁智者见智一下我是我支构造函数的理由 保证一些重要的属性在
对Spring中接口注入的理解实例分析 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Type接
分享Spring中接口注入的三种方式 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 下面是Spr
Spring的三种注入方式都是什么?如何选用? 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!接口注
cha138/Article/program/Java/JSP/201311/20117
知识大全 Spring强制向servlet中注入bean的方法
Spring强制向servlet中注入bean的方法 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!