我也来实现ioc

前两天看到论坛上有人实现的ioc功能,也就自己实现了一个基本一样的.

public class MyCompiler {
ArrayList<BeanModel> resource=new ArrayList<BeanModel>();

HashMap<String,Object> oo=new HashMap<String,Object>();

public void parseThis() throws SAXException, DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException{

SAXReader sr=new SAXReader();

Document document=sr.read(new File("path"));
System.out.println("mycompiler");
Element root=document.getRootElement();
List thislist=root.elements("bean");
Iterator ii=thislist.iterator();
while(ii.hasNext()){

BeanModel bm=new BeanModel();
Element idNode=(Element)ii.next();
String id=idNode.attribute("id").getValue();
String className=idNode.attribute("class").getValue();
bm.setClassName(className);
bm.setId(id);
HashMap<String,String> propertyNames=new HashMap<String,String>();
List refList=idNode.elements("property");
Iterator iiii= refList.iterator();
while(iiii.hasNext()){
Element eee=(Element)iiii.next();
String name=eee.attributeValue("name");
String ref=eee.attributeValue("ref");
propertyNames.put(name, ref);
}
bm.setPropertyNames(propertyNames);
resource.add(bm);

}



}
public void instanceAllRef() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException{
for(BeanModel bm:resource){
String id=bm.getId();
String className=bm.getClassName();

oo.put(id,Class.forName(className).newInstance());

}
for(BeanModel bm:resource){
String id=bm.getId();
String className=bm.getClassName();
HashMap<String,String> thisMap=bm.getPropertyNames();
Set<Map.Entry<String, String>> set = thisMap.entrySet();
Iterator<Map.Entry<String, String>> it=set.iterator();
while(it.hasNext()){
Map.Entry<String,String> entry=it.next();
String name=entry.getKey();
String ref=entry.getValue();
Object obj= oo.get(ref);
String setterName="set"+name.substring(0,1).toUpperCase()+name.substring(1);
Method method=oo.get(bm.getId()).getClass().getMethod(setterName,obj.getClass().getInterfaces());//传入属性的接口和class都行 接口更加广泛
method.invoke(oo.get(bm.getId()), obj);
}



}}

public MyCompiler() throws SAXException, DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, IllegalArgumentException, NoSuchMethodException, InvocationTargetException{
this.parseThis();
this.instanceAllRef();
}




public Object getBean(String id){

return this.oo.get(id);

}


}



BeanModel


public class BeanModel {
private String id;
private String className;
private HashMap<String,String> propertyNames;
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 HashMap<String, String> getPropertyNames() {
return propertyNames;
}
public void setPropertyNames(HashMap<String, String> propertyNames) {
this.propertyNames = propertyNames;
}


}


只求实现基本的思路和流程.
然后发现反射的时候,如果method的参数指定某一个类的class比如我这边Method method=oo.get(bm.getId()).getClass().getMethod(setterName,obj.getClass().getInterfaces() 改成Method method=oo.get(bm.getId()).getClass().getMethod(setterName,obj.getClass()那么传入的参数是实现类的时候是不能实现的,如果传入参数改成接口或者superclass(),因为getInterfaces()返回的是一个class数组,也是满足参数的类型.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值