beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="userDao" class="com.test.dao.impl.UserDaoImpl" /> <bean id="userService" class="com.test.service.UserService"> <property name="userDao" bean="userDao" /> </bean> </beans>
UserDao
package com.test.dao; import com.test.entity.User; public interface UserDao { public void save(User u); }
UserDaoImpl
package com.test.dao.impl; import com.test.dao.UserDao; import com.test.entity.User; public class UserDaoImpl implements UserDao { public void save(User u) { System.out.println("save a user !"); } }
User
package com.test.entity; public class User { private int uid; private String uname; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } }
UserService
package com.test.service; import com.test.dao.UserDao; import com.test.entity.User; public class UserService { private UserDao userDao; public UserDao getUserDao() { return userDao; } public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void save(User u) { userDao.save(u); } }
BeanFactory
package myspring; public interface BeanFactory { public Object getBean(String bean); }
ClassPathXmlApplicationContext
package myspring; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; public class ClassPathXmlApplicationContext implements BeanFactory { private Map<String, Object> beans = new HashMap<String, Object>(); @SuppressWarnings("unchecked") public ClassPathXmlApplicationContext () throws JDOMException, IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException { SAXBuilder sax = new SAXBuilder(); Document doc = sax.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml")); Element element = doc.getRootElement(); List<Element> list = element.getChildren("bean"); Element e = null; for (int i = 0; i < list.size(); i++) { e = list.get(i); String id = e.getAttributeValue("id"); String clazz = e.getAttributeValue("class"); Object o = Class.forName(clazz).newInstance(); beans.put(id, o); for (Element propElement : (List<Element>)e.getChildren("property")) { String name = propElement.getAttributeValue("name"); String bean = propElement.getAttributeValue("bean"); System.out.println(name + " " + bean); Object beanObject = beans.get(bean); String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); System.out.println(methodName); Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]); m.invoke(o, beanObject); } } } public Object getBean(String bean) { return this.beans.get(bean); } }
UserServiceTest(JUnit)
package com.test.service; import static org.junit.Assert.*; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import myspring.BeanFactory; import myspring.ClassPathXmlApplicationContext; import org.jdom.JDOMException; import org.junit.Test; import com.test.dao.UserDao; import com.test.entity.User; public class UserServiceTest { @Test public void testSave() { BeanFactory cpxac = null; try { cpxac = new ClassPathXmlApplicationContext(); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } UserService uService = (UserService)cpxac.getBean("userService"); // UserDao userDao = (UserDao)cpxac.getBean("userDao"); // uService.setUserdao(userDao); User u = new User(); uService.save(u); } }
到此结束