模拟依赖注入,首先用dom4j解析xml文档。先在单例模式实例化工厂方法的时候获得全部节点bean,用map3将之存起来。之后再在getbean方法内定义map和map2,分别用来存储参数传来的方法名和bean的子节点property下的所有name和ref。最后,迭代map3查找map3下与map2的ref相同名的bean得 id,用PropertyDescriptor[]ps=Introspector.getBeanInfo(o.getClass()).getPropertyDescriptors();
Method method=pp.getWriteMethod();
method.INVOKE方法来注入实例
package cn.com.ioc;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import cn.com.dao.impl.caocaoDaoImpl;
public class IOCFactory {
private static IOCFactory iocfactory;
private String fileName;
public static Map<String,String> map3=new HashMap<String, String>();
private IOCFactory(){}
private IOCFactory(String s){
this.fileName=s;
}
public synchronized static IOCFactory getIOCFactory(String s){
if(iocfactory==null){
iocfactory=new IOCFactory(s);
}
try {
SAXReader reader=new SAXReader();
InputStream in=new FileInputStream(iocfactory.fileName);
Document doc=reader.read(in);
Element root=doc.getRootElement();
List<Element> l=root.elements();
//罗列出所有的bean以及子类
for(Element e:l){
String id = e.attributeValue("id");
String classs=e.attributeValue("class");
if(id!=null&&classs!=null)
map3.put(id, classs);
}
} catch (Exception e) {
e.printStackTrace();
}
return iocfactory;
}
public Object getBean(String s){
Object o=new Object();
Map<String,Object> map=new HashMap<String, Object>();
Map<String,String> map2=new HashMap<String, String>();
try {
SAXReader reader=new SAXReader();
System.out.println(iocfactory.fileName);
InputStream in=new FileInputStream(iocfactory.fileName);
Document doc=reader.read(in);
Element root=doc.getRootElement();
List<Element> l=root.elements();
//罗列出所有的bean以及子类
for(Element e:l){
String id = e.attributeValue("id");
String classs=e.attributeValue("class");
if("bean".equals(e.getName())&&id.equals(s)){
System.out.println("haha");
map.put(id, classs);
List<Element> l2=e.elements();
System.out.println(l2.size());
for(Element e2:l2){
String name=e2.attributeValue("name");
String ref=e2.attributeValue("ref");
System.out.println(ref);
if(name!=null&&ref!=null&&"property".equals(e2.getName())){
System.out.println("hehe");
map2.put(name, ref);
}
}
}
}
for(String ss:map.keySet()){
System.out.println(ss+" : "+map.get(ss));
String classname=(String) map.get(ss);
Class c=Class.forName(classname);
o=c.newInstance();
PropertyDescriptor[] ps=Introspector.getBeanInfo(o.getClass()).getPropertyDescriptors();
for(String sss:map3.keySet()){
System.out.println("map3迭代:"+sss+":"+map3.get(sss));
for(String mp2:map2.keySet()){
for(PropertyDescriptor pp:ps){
System.out.println("属性名:"+pp.getName()+" sss:"+sss+" iseq:"+pp.getName().equals(sss));
if(pp.getName().equals(mp2)&&pp.getName().equals(sss)){
Method method=pp.getWriteMethod();
Object value=Class.forName(map3.get(sss)).newInstance();
method.invoke(o, value);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return o;
}
}
以上是工厂方法,还有services方法和dao方法
package cn.com.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import cn.com.connection.JDBCConnection;
import cn.com.dao.caocaoDao;
import cn.com.entity.Fenlei;
public class caocaoDaoImpl implements caocaoDao{
@Override
public void add() {
Connection con=null;
ResultSet rs=null;
PreparedStatement ps=null;
try {
con=JDBCConnection.getConnection();
String s="insert into fenlei(name) values(?)";
ps=con.prepareStatement(s);
ps.setString(1, "XXX");
System.out.println("XXX启动");
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public List findAll() {
Connection con=null;
ResultSet rs=null;
PreparedStatement ps=null;
List list=new ArrayList();
try {
con=JDBCConnection.getConnection();
String s="select * from fenlei";
ps=con.prepareStatement(s);
rs=ps.executeQuery();
while(rs.next()){
Fenlei fenlei=new Fenlei();
fenlei.setFenlei_id(rs.getInt("fenlei_id"));
fenlei.setName(rs.getString("name"));
list.add(fenlei);
}
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
package cn.com.service;
import javax.annotation.Resource;
import cn.com.dao.impl.caocaoDaoImpl;
import cn.com.entity.Fenlei;
public class caocaoServices {
private caocaoDaoImpl caocaoDao;
public caocaoDaoImpl getCaocaoDao() {
return caocaoDao;
}
@Resource
public void setCaocaoDao(caocaoDaoImpl caocaoDao) {
this.caocaoDao = caocaoDao;
}
}
再后是xml文件
<pre name="code" class="html"><?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<bean id="caocaoDao" class="cn.com.dao.impl.caocaoDaoImpl"></bean>
<bean id="caocaoServices" class="cn.com.service.caocaoServices" scope="singleton">
<property name="fenlei" ref="fenlei"></property>
<property name="caocaoDao" ref="caocaoDao"></property>
</bean>
<bean id="fenlei" class="cn.com.entity.Fenlei"></bean>
</beans>
最后是测试类
package cn.com.spring;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.com.ioc.IOCFactory;
import cn.com.service.caocaoServices;
public class caocao {
// @Test
// protected void getSpring(){
// ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
// caocaoServices c=(caocaoServices) ctx.getBean("caocaoServices");
// List list=c.getCaocaoDao().findAll();
// System.out.println(list.size());
// }
@Test
public void getIOC(){
IOCFactory ioc=IOCFactory.getIOCFactory("src\\beans.xml");
System.out.println("sadas");
caocaoServices c=(caocaoServices) ioc.getBean("caocaoServices");
c.getCaocaoDao().add();
}
}