手動編寫SpringIOC框架

一.什麼是SpringIOC

什麼是SpringIOC,就是把每一個bean(實體類)與bean(實體了)之間的關係交給第三方容器進行管理。

Xml配置:

<beans>
	<bean id="user1" class="com.itmayiedu.entity.UserEntity">
		<property name="userId" value="0001"></property>
		<property name="userName" value="餘勝軍"></property>
	</bean>
	<bean id="user2" class="com.itmayiedu.entity.UserEntity">
		<property name="userId" value="0002"></property>
		<property name="userName" value="張三"></property>
	</bean>
</beans>

Java代碼:

//1.讀取springxml配置
		ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		//2.獲取bean對象
		TestService testService = (TestService) classPathXmlApplicationContext.getBean("testService");
		System.out.println(testService.test());

二.什麼是SpringIOC底層實現原理

1.讀取beanXML配置文件

2.使用beanId查找bean配置,並獲取配置文件中class地址。

3.使用Java反射技術實例化對象

4.獲取屬性配置,使用反射技術進行賦值。

詳細步驟:

(1).利用傳入的參數獲取xml文件的流,並且利用dom4j解析成Document對象

2).對於Document對象獲取根元素對象<beans>後對下面的<bean>標籤進行遍歷,判斷是否有符合的id.

3).如果找到對應的id,相當於找到了一個Element元素,開始創建對象,先獲取class屬性,根據屬性值利用反射建立對象.

4).遍歷<bean>標籤下的property標籤,並對屬性賦值.注意,需要單獨處理int,float類型的屬性.因爲在xml配置中這些屬性都是以字符串的形式來配置的,因此需要額外處理.

5).如果屬性property標籤有ref屬性,說明某個屬性的值是一個對象,那麼根據id(ref屬性的值)去獲取ref對應的對象,再給屬性賦值.

6.返回建立的對象,如果沒有對應的id,或者<beans>下沒有子標籤都會返回null

三.建立實體類:

public class User {

	private String userId;
	private String userName;
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
}

四.使用反射技術完成Java代碼(這就是springioc的底層代碼):

import java.lang.reflect.Field;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.itmayiedu.entity.User;

/**
 * 
 * @classDesc: 功能描述:(讀取Spring配置文件)
 * @author: 餘勝軍
 * @createTime: 2017年8月25日 上午1:27:55
 * @version: v1.0
 * @copyright:上海每特教育科技有限公司
 */
public class ClassPathXmlApplicationContext {
	private String xmlPath;

	/**
	 * 
	 * @param xmlPath
	 *            spring xml 配置路徑
	 */
	public ClassPathXmlApplicationContext(String xmlPath) {
		this.xmlPath = xmlPath;
	}

	public Object getBean(String beanId) throws Exception {

		// 解析xml器
		SAXReader saxReader = new SAXReader();
		Document read = null;
		try {
			// 從項目根目錄路徑下 讀取
			read = saxReader.read(this.getClass().getClassLoader().getResourceAsStream(xmlPath));
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (read == null) {
			return null;
		}
		// 獲取根節點資源
		Element root = read.getRootElement();
		List<Element> elements = root.elements();
		if (elements.size() <= 0) {
			return null;
		}
		Object oj = null;
		for (Element element : elements) {
			String id = element.attributeValue("id");
			if (StringUtils.isEmpty(id)) {
				return null;
			}
			if (!id.equals(beanId)) {
				continue;
				// throw new Exception("使用beanId:" + beanId + ",未找到該bean");
			}
			// 獲取實體bean class地址
			String beanClass = element.attributeValue("class");
			// 使用反射實例化bean
			Class<?> forNameClass = Class.forName(beanClass);
			oj = forNameClass.newInstance();
			// 獲取子類對象
			List<Element> attributes = element.elements();
			if (attributes.size() <= 0) {
				return null;
			}
			for (Element et : attributes) {
				// 使用反射技術爲方法賦值
				String name = et.attributeValue("name");
				String value = et.attributeValue("value");
				Field field = forNameClass.getDeclaredField(name);
				field.setAccessible(true);
				field.set(oj, value);

			}

		}
		return oj;
		// 1.使用beanId查找配置文件中的bean。
		// 2.獲取對應bean中的classpath配置
		// 3.使用java反射機制實體化對象
	}

	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		User bean = (User) applicationContext.getBean("user2");
		System.out.println("使用反射獲取bean" + bean.getUserId() + "---" + bean.getUserName());

	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章