通过反射模拟springIoc

模拟springIOC

文章目录

  • 模拟springIOC
    • 1.jar包准备
    • 2.目录结构![在这里插入图片描述](https://img-blog.csdnimg.cn/20190602102253992.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTU4NDI3Mg==,size_16,color_FFFFFF,t_70)
    • 3.Dao层
    • 4.DaoImpl层
    • 5.实体
    • 6.业务层
    • 7.BeanFactory
    • 8.配置文件
    • 9.ClassPathXmlApplicationContext

1.jar包准备


通过jdom读取配置文件

2.目录结构通过反射模拟springIoc_第1张图片

3.Dao层

// An highlighted block
public interface UserDao {
  public void save(User user);
}

4.DaoImpl层

// An highlighted block
public class UserDaoImpl implements UserDao {
	@Override
	public void save(User user) {
		System.out.println("用户"+user+"被保存");
	}
}

5.实体

// An highlighted block
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;
	}
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(String username, String password) {
		super();
		this.username = username;
		this.password = password;
	}
	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + "]";
	}

6.业务层

// An highlighted block
public class UserService {

	private UserDao userDao;

	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public void addUser(User u){
		this.userDao.save(u);
	}
}

7.BeanFactory

// An highlighted block
public interface BeanFactory {
	public Object getBean(String id);
}

8.配置文件

// An highlighted block
<?xml version="1.0" encoding="UTF-8"?>
<beans>
		<bean id="userDao" class="dao.impl.UserDaoImpl"></bean>
		<bean id="userService" class="service.UserService">
		<property name="userDao" ref="userDao"></property>
		</bean>
</beans>

9.ClassPathXmlApplicationContext

// An highlighted block
public class ClassPathXmlApplicationContext implements BeanFactory{

	@Override
	public Object getBean(String id) {
		return map.get(id);
	}
Map<String,Object> map=new HashMap<String,Object>();
	public ClassPathXmlApplicationContext(String config) throws Exception{
		SAXBuilder sbx = new SAXBuilder();
		Document doc = sbx.build(this.getClass().getClassLoader().getResourceAsStream(config));
		Element root = doc.getRootElement();// 获取   节点
		List<Element> childs = (List<Element>) root.getChildren("bean");//获取子节点
		for(Element element:childs){
			String id=element.getAttributeValue("id");//子节点上的属性id
			String clazz = element.getAttributeValue("class");//子节点上的属性class
			Class<?> forName = Class.forName(clazz);//通过反射获取字节码文件
			Object object = forName.getConstructor().newInstance();//通过类创建对象
			map.put(id,object);//将对象 id存入map中
			
			List<Element> properties = (List<Element>) element.getChildren("property");//获取下的节点property
			for(Element e:properties){
				String name = e.getAttributeValue("name");//子节点上的属性name
				String ref = e.getAttributeValue("ref");//子节点上的属性ref
				Object reff = map.get(ref);//根据id在map中查找对象
				String methodName = "set"+ name.substring(0,1).toUpperCase()+name.substring(1);//setUserDao()
				Method method = object.getClass().getMethod(methodName, reff.getClass().getInterfaces()[0]);//getMethod参数为方法名和方法参数类
			    method.invoke(object, reff);
			}
		}
	}
	
}

你可能感兴趣的:(通过反射模拟springIoc)