Java模拟Spring理解Spring的原理

通过这个实例,1)理解Spring配置文件中容器的概念

                         2)学会读取Spring中的配置文件,学会读取xml文件

                         

对象类:User.java

服务类:UserService.java

操作数据库:UserDAO

操作数据库的实现类:UserDAOImpl

因为可能有多个业务,增加用户,增加教师,增加学生:所以需要利用工程模式。需要谁我就用谁的工厂

BeanFactory.java

工厂的实现:ClassPathXmlApplicationContext.java      在这里面我们实现调用xml中的内容,读取xml文件

配置文件:beans.xml



主要代码:ClassPathXmlApplicationContext.java  

package spring;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class ClassPathXmlApplicationContext implements BeanFactory {
	
	private Map<String , Object> beans = new HashMap<String, Object>();
	//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();  
	    List list=root.getChildren("bean"); 
	    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 name) {
		return beans.get(name);
	}

}

1)对m.invoke(o, beanObject);这段代码的理解

o这个对象,调用m这个方法,将beanObject这个参数传递进去。

2)理解

 Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);




xml文件:beans.xml

<beans>
	<bean id="u" class="dao.impl.UserDAOImpl" />
	<bean id="userService" class="service.UserService" >
		<property name="userDAO" bean="u"/>
	</bean>
	
</beans>
Spring里面有一个非常重要的概念:以来注入,这里通过property参数,就可以将UserDAOImpl和userService确定下来。


这两个文件要好好的对应看看,1)看看是怎么读取文件里面的内容的2)怎么对应的3)反射4)必须理解beanx.xml中beans是容器的概念

其它文件如下:

User.java

package model;

public class User {
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

UserDAO.java

package dao;

import model.User;


public interface UserDAO {
	public void save(User user);
}

UserDAOImpl.java

package dao.impl;

import model.User;
import dao.UserDAO;

public class UserDAOImpl implements UserDAO {

	public void save(User user) {
		//Hibernate
		//JDBC
		//XML
		//NetWork
		System.out.println("user saved!");
	}

}


BeanFactory.java

package spring;

public interface BeanFactory {
	public Object getBean(String name);
}


测试:

测试中我们需要的UserService时,是从工厂中拿的,我们并没有new

package test;
import model.User;
import org.junit.Test;
import service.UserService;
import spring.BeanFactory;
import spring.ClassPathXmlApplicationContext;
public class UserServiceTest {
	@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);
	}

}

对于测试,需要理解
ClassPathXmlApplicationContext()理解上下文概念。通过上下文得到Bean中的内容。

你可能感兴趣的:(Java模拟Spring)