前两天看到论坛上有人实现的ioc功能,也就自己实现了一个基本一样的.
BeanModel
只求实现基本的思路和流程.
然后发现反射的时候,如果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数组,也是满足参数的类型.
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数组,也是满足参数的类型.