模拟Spring的 IOC 和 DI 博客分类: Spring 3.2 iocspringdi
程序员文章站
2024-02-06 20:25:34
...
模拟Spring的 IOC 和 DI
1.BeanFactory 接口
2.ClassPathXmlApplicationContex实现类
3.applicationContext-beans.xml配置文件
4.UserDAO接口和UserDAOImpl实现类
5.UserService接口和实现类UserServiceImpl
6.测试类
1.BeanFactory 接口
public interface BeanFactory { public Object getBean(String id); }
2.ClassPathXmlApplicationContex实现类
public class ClassPathXmlApplicationContext implements BeanFactory { private Map<String , Object> beans = new HashMap<String, Object>(); /** * 模拟控制反转(IOC)+依赖注入(DI) * IOC Inverse of Control * DI Dependency Injection */ public ClassPathXmlApplicationContext() throws Exception { SAXBuilder sb=new SAXBuilder(); Document doc=sb.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml")); //构造文档对象 Element root=doc.getRootElement(); //获取根元素HD List list=root.getChildren("bean");//取名字为disk的所有元素 for(int i=0;i<list.size();i++){ Element element=(Element)list.get(i); String id=element.getAttributeValue("id"); String clazz=element.getAttributeValue("class"); Object o = Class.forName(clazz).newInstance(); System.out.println(id); System.out.println(clazz); beans.put(id, o); for(Element propertyElement : (List<Element>)element.getChildren("property")) { String name = propertyElement.getAttributeValue("name"); //userDAO String bean = propertyElement.getAttributeValue("bean"); //u Object beanObject = beans.get(bean);//UserDAOImpl instance String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); System.out.println("method name = " + methodName); Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]); m.invoke(o, beanObject); } } } public Object getBean(String id) { return beans.get(id); } }
3.applicationContext-beans.xml配置文件
<beans> <bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl" /> <bean id="userService" class="com.bjsxt.service.UserService" > <property name="userDAO" bean="u"/> </bean> </beans>
4.UserDAO接口和UserDAOImpl实现类
public interface UserDAO { public void save(User user); } public class UserDAOImpl implements UserDAO { public void save(User user) { //Hibernate //JDBC //XML //NetWork System.out.println("user saved!"); } }
5.UserService接口和实现类UserServiceImpl
public interface UserService { public void add(User user); } public class UserServiceImpl implements UserService{ private UserDAO userDAO; public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } }
6.测试类
import com.momo.spring.BeanFactory; import com.momo.spring.ClassPathXmlApplicationContext; public class IOCTest { @Test public void testAdd() throws Exception { BeanFactory applicationContext = new ClassPathXmlApplicationContext(); UserService service = (UserService)applicationContext.getBean("userService"); User u = new User(); u.setUsername("zhangsan"); u.setPassword("zhangsan"); service.add(u); } } import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class IOCTest { @Test public void testAdd() throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");//BeanFactory也可以 UserService service = (UserService)ctx.getBean("userService"); User u = new User(); u.setUsername("zhangsan"); u.setPassword("zhangsan"); service.add(u); } }