模拟spring框架注入实现原理
定义一些抽象的方法:
[java]
package com.huxin.springinject.dao;
public inte易做图ce Person {
public void save();
public void useAxe();
}
package com.huxin.springinject.dao;
public inte易做图ce Person {
public void save();
public void useAxe();
}
[java]
package com.huxin.springinject.dao;
public inte易做图ce Axe {
public void chop();
}
package com.huxin.springinject.dao;
public inte易做图ce Axe {
public void chop();
}
实现的一些方法:
[java]
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;
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();
}
}
[java]
package com.huxin.springinject.dao.impl;
import com.huxin.springinject.dao.Axe;
public class StoneAxe implements Axe {
@Override
public void chop() {
System.out.println("铁斧头砍柴真慢");
}
}
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框架模拟的实现的一些原理!!!
[java]
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"));
&n
补充:软件开发 , Java ,