欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring IOC(DI)模拟 博客分类: Web后端 springiocbeandi依赖注入 

程序员文章站 2024-02-06 20:21:58
...
Spring DI Simulation

解释:IOC Inverse Of Control(控制反转) 或者 DI Dependency Injection(依赖注入)
观察以下代码,发现UserService类并没有对自己的属性UserDAO进行初始化,但是UserDAO仍然不为null,因为ClassPathXmlApplicationContext类初始化的时候,通过读取配置文件bean.xml将UserDAO对象注入到UserService对象里面。所以明面上没有对其进行初始化,使用配置文件初始化类对象,易于控制,不必修改类代码,可扩展性强。
User类
package com.edu.hpu.model;

public class User {

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

UserDAO接口
package com.edu.hpu.dao;

import com.edu.hpu.model.User;

public interface UserDAO {

	public void save(User user);
	
}
UserDAOImpl类Implements UserDAO
package com.edu.hpu.impl;

import com.edu.hpu.dao.UserDAO;
import com.edu.hpu.model.User;

public class UserDAOImpl implements UserDAO {

	public void save(User user) {
		System.out.println("user saved!");
	}
	
}

4、UserService类
package com.edu.hpu.service;

import com.edu.hpu.dao.UserDAO;
import com.edu.hpu.model.User;

public class UserService {

	private UserDAO userDAO;
	
	public void save(User user) {
		userDAO.save(user);
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
}

BeanFactory接口
package com.hpu.spring;

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

ClassPathXmlApplicationContext类 Implements BeanFactory(重点)
package com.hpu.spring;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import com.edu.hpu.dao.UserDAO;

public class ClassPathXmlApplicationContext implements BeanFactory {

	Map<String , Object> beans = new HashMap<String , Object>();
	
	public ClassPathXmlApplicationContext() {
		SAXBuilder sax = new SAXBuilder();
		try {
			Document doc = sax.build("beans.xml");
			Element root = doc.getRootElement();
			
			List<Element> children = root.getChildren();
			for(int i = 0; i < children.size(); i++) {
				Element child = children.get(i);
				
				String key = child.getAttributeValue("id");
				String className = child.getAttributeValue("class");
				//System.out.println(key + "   " + className);
				Object value = Class.forName(className).newInstance();
				//System.out.println(value);
				beans.put(key, value);
				
				for(Element pro : child.getChildren("property")) {
					if(pro != null) {
						String name = pro.getAttributeValue("name");
						String beanName = pro.getAttributeValue("bean");
						char c = name.charAt(0);
						c = Character.toUpperCase(c);
						name = "set" + c + name.substring(1);
						Object bean = beans.get(beanName);
						Method methodName = value.getClass().getMethod(name, bean.getClass().getInterfaces()[0]);
						methodName.invoke(value, bean);
						System.out.println(name);
						System.out.println(bean);
					}
				}
			}
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch(ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public Object getBean(String name) {
		return beans.get(name);
	}
}

bean.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<bean id="u" class="com.edu.hpu.impl.UserDAOImpl"></bean>
	
	<bean id="userService" class="com.edu.hpu.service.UserService">
		<property name="userDAO" bean="u"/>
	</bean>
</beans>

测试类
package com.edu.hpu.sevice;

import org.junit.Test;

import com.edu.hpu.model.User;
import com.edu.hpu.service.UserService;
import com.hpu.spring.BeanFactory;
import com.hpu.spring.ClassPathXmlApplicationContext;

public class TestUser {

	@Test
	public void testSave() {
		BeanFactory bf = new ClassPathXmlApplicationContext();
		
		UserService service = (UserService)bf.getBean("userService");
		User u = new User();
		service.save(u);
	}
	
}