模拟spring框架注入实现原理
这个我是参见了别人的一些东西,不是原创!
定义一些抽象的方法:
package com.huxin.springinject.dao;
public interface Person {
public void save();
public void useAxe();
}
package com.huxin.springinject.dao;
public interface Axe {
public void chop();
}
实现的一些方法:
package com.huxin.springinject.dao.impl; import com.huxin.springinject.dao.Axe;
import com.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 com.huxin.springinject.dao.impl;
import com.huxin.springinject.dao.Axe;
public class StoneAxe implements Axe {
@Override
public void chop() {
System.out.println("铁斧头砍柴真慢");
}
}
这里是关键spring框架模拟的实现的一些原理!!!
package junit.test;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
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.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.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","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){
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 = ConvertUtils.convert(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.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;
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;
import com.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="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="chinese" class="com.huxin.springinject.dao.impl.Chinese">
<property name="axe" ref="stoneAxe"/>
</bean>
<bean id="stoneAxe" class="com.huxin.springinject.dao.impl.StoneAxe"/>
</beans>
补充说明: 需要导入dom4j相应的辅助包和junit辅助包!
模拟spring框架注入实现原理的更多相关文章
- 采用dom4j和反射模拟Spring框架的依赖注入功能
Spring的依赖注入是指将对象的创建权交给Spring框架,将对象所依赖的属性注入进来的行为.在学习了dom4j后,其实也可以利用dom4j和反射做一个小Demo模拟Spring框架的这种功能.下面 ...
- Spring框架和MVC原理
Spring框架和MVC原理 目录 Spring框架 SpringMVC工作原理 参考资料 回到顶部 Spring框架 Spring当前框架有20个jar包,大致可以分为6大模块: Core Cont ...
- (转)编码剖析Spring依赖注入的原理
http://blog.csdn.net/yerenyuan_pku/article/details/52834561 Spring的依赖注入 前面我们就已经讲过所谓依赖注入就是指:在运行期,由外部容 ...
- Spring、Spring依赖注入与编码剖析Spring依赖注入的原理
Spring依赖注入 新建PersonIDao 和PersonDao底实现Save方法: public interface PersonIDao { public void save(); } pub ...
- 【Spring系列】- 手写模拟Spring框架
简单模拟Spring 生命不息,写作不止 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 前言 上次已经学习了 ...
- 模拟Spring依赖注入
通过读取xml文件,利用反射动态加载类和方法,其实就是spring的注入机制模拟,可以清晰的看出整个运行流程 1.配置文件 applicationContext.xml <beans> & ...
- Spring框架介绍和原理
SpringMVC框架介绍 1) Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面. Spring 框架提供了构建 Web 应用程序的全功 ...
- 简单模拟Spring的注入
主要就是读XML技术和反射技术. 在xml中读出相关配置信息,然后利用反射将其实例化为对象,并调用其构造方法,在实例化的过程中将属性注入实例. 实例化和属性注入这些操作都交给了框架,不再需要自己的去n ...
- spring @Autowired注入的原理
只知道如何用Autowired注解,知道可以替代set,get方法,很方便,却一直不知道,为什么可以代替 今天探索一下原因,所谓知其然还要知其所以然,才能理解的更好,记忆的更牢,才能转化为自己的知识. ...
随机推荐
- 基于visual Studio2013解决C语言竞赛题之1016循环打印矩阵
题目 解决代码及点评 /* 找规律,编程序打印6×6的矩阵: 1 2 3 5 8 13 1 4 9 17 30 51 1 6 19 45 92 173 . . . */ ...
- B树的实现与源代码二(删除源代码)
int BTreeMaximum( BNode *x ) { if ( x->leaf ) { return x->key[x->size - 1]; } else { return ...
- 算法-最长子序列和C/C++实现(三个复杂度)
最长子序列和的问题非常easy: 就是一个数组,求出当中当中连续的某一段和,而这一段和是全部的连续段和的最大的值.求出这个值. 先说复杂度最高的:O(n3) 直接上代码,非常easy的: // // ...
- Visual Studio2013创建、公布监控Windows Azure网站
原文 Visual Studio2013创建.公布监控Windows Azure网站 随着Visual Studio 2013的发布,现在我们可以在Visual Studio内部实现Windows A ...
- 阿里巴巴 web前端性能优化进阶路
Web前端性能优化WPO,相信大多数前端同学都不会陌生,在各自所负责的站点页面中,也都会或多或少的有过一定的技术实践.可以说,这个领域并不缺乏成熟技术理论和技术牛人:例如Yahoo的web站点性能优化 ...
- 为什么要用BitSet
BitSet适用于一类型boolean判断,Java的BitSet在这类型判断中非常高效. 举例说明:在判断前2000万数字中素数个数的程序中,如果使用最基本的素数判断代码: package com; ...
- hdu 1760 一道搜索博弈题 挺新颖的题目
A New Tetris Game Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- HDU 4709 Herding 几何题解
求全部点组成的三角形最小的面积,0除外. 本题就枚举全部能够组成的三角形,然后保存最小的就是答案了.由于数据量非常少. 复习一下怎样求三角形面积.最简便的方法就是向量叉乘的知识了. 并且是二维向量叉乘 ...
- Android下QQ空间查看大图特效
近期在做一个项目,里面有一个功能是实现Android QQ好友动态里面的缩略图放大,查看大图的效果.用过都知道,这个特效非常赞的,没用过的下载个玩玩吧.我刚開始以为放大的那个大图是一个Activity ...
- Cordys BOP 4平台开发入门实战演练——Webservices开发(0基础)
0.文章导读 本文档针对Cordys BOP-4 WS-AppServer基础功能进行验证和高速开发指导.(高级实践文档请參考兴许文档). 0.1.WS-AppServer概述 WS-AppServe ...