BeanFactory
1 package com.bjsxt.spring; 2 3 public interface BeanFactory { 4 public Object getBean(String name); 5 }
ClassPathXmlApplicationContext
1 package com.bjsxt.spring; 2 3 import java.lang.reflect.Method; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import org.jdom.Document; 9 import org.jdom.Element; 10 import org.jdom.input.SAXBuilder; 11 12 public class ClassPathXmlApplicationContext implements BeanFactory{ 13 private Map<String, Object> beans = new HashMap<String, Object>(); 14 15 public ClassPathXmlApplicationContext() throws Exception{ 16 SAXBuilder sb = new SAXBuilder(); 17 Document doc = sb.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml")); 18 Element root = doc.getRootElement();//获取根元素 19 List list = root.getChildren("bean");//取名字为bean的所有元素 20 for(int i=0; i<list.size(); i++){ 21 Element element = (Element) list.get(i); 22 String id = element.getAttributeValue("id"); 23 String clazz = element.getAttributeValue("class"); 24 System.out.println(id + ":" + clazz); 25 Object o = Class.forName(clazz).newInstance(); 26 beans.put(id, o); 27 28 for(Element propertyElement : (List<Element>)element.getChildren("property")){ 29 String name = propertyElement.getAttributeValue("name");//userDAO 30 String bean = propertyElement.getAttributeValue("bean");//u 31 Object beanObject = beans.get(bean);//UserDAOImpl instance 32 33 String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); 34 System.out.println("method name = " + methodName); 35 36 Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]); 37 m.invoke(o, beanObject); 38 } 39 } 40 } 41 42 public Object getBean(String name) { 43 return beans.get(name); 44 } 45 46 }
beans.xml
1 <beans> 2 <bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl"></bean> 3 <bean id="userService" class="com.bjsxt.service.UserService"> 4 <property name="userDAO" bean="u"></property> 5 </bean> 6 </beans>
UserDAO
1 package com.bjsxt.dao; 2 3 import com.bjsxt.model.User; 4 5 public interface UserDAO { 6 public void save(User u); 7 }
UserDAOImpl
1 package com.bjsxt.dao.impl; 2 3 import com.bjsxt.dao.UserDAO; 4 import com.bjsxt.model.User; 5 6 public class UserDAOImpl implements UserDAO{ 7 8 public void save(User u) { 9 System.out.println("a user saved!"); 10 } 11 12 }
测试类:
1 package com.bjsxt.service; 2 3 import org.junit.Test; 4 5 import com.bjsxt.model.User; 6 import com.bjsxt.spring.BeanFactory; 7 import com.bjsxt.spring.ClassPathXmlApplicationContext; 8 9 public class UserServiceTest { 10 11 @Test 12 public void testAddUser() throws Exception { 13 BeanFactory factory = new ClassPathXmlApplicationContext(); 14 15 UserService service = (UserService)factory.getBean("userService"); 16 User u = new User(); 17 service.addUser(u); 18 } 19 20 }
代码链接: http://pan.baidu.com/s/1eSoYu8i 密码: gtj2
所需jar包:
junit链接: http://pan.baidu.com/s/1kVFS8lx 密码: mkh3
jdom链接: http://pan.baidu.com/s/1c1X15Xu 密码: 9vpi
时间: 2024-10-31 04:12:23