手寫spring ioc框架
user.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="user1" class="com.huishao.entity.UserEntity">
<property name="userId" value="0001"></property>
<property name="userName" value="張三"></property>
</bean>
<bean id="user2" class="com.huishao.entity.UserEntity">
<property name="userId" value="0002"></property>
<property name="userName" value="李四"></property>
</bean>
</beans>
userEntity.java
package com.huishao.entity;
public class UserEntity {
private String userId;
private String userName;
public UserEntity(){
System.out.println("使用反射技術 ,執行無引數構造 函式");
}
public UserEntity(String userId) {
this.userId=userId;
System.out.println("使用反射技術 執行 有參建構函式 userId:"+userId);
}
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;
}
}
ClassPathXmlApplicationContext.java
package com.huishao.spring;
import java.lang.reflect.Field;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.huishao.entity.UserEntity;
public class ClassPathXmlApplicationContext {
private String xmlPath;
public ClassPathXmlApplicationContext(String xmlPath) {
this.xmlPath = xmlPath;
}
public Object getBean(String beanId) throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException {
//1、讀取xml配置檔案
// 獲取xml解析器
SAXReader saxReader = new SAXReader();
// 獲得document物件
Document read = saxReader.read(this.getClass().getClassLoader().getResourceAsStream(xmlPath));
// 獲得根節點
Element rootElement = read.getRootElement();
System.out.println("根節點的名稱: " +rootElement.getName());
//獲得元素物件
List<Element> elements = rootElement.elements();
Object obj = null;
for (Element sonEle : elements) {
//2、獲取到每個bean配置,獲得class地址
//獲得每個bean配置 獲取class地址
String sonBeanId = sonEle.attributeValue("id");
if(!beanId.equals(sonBeanId)){
continue;
}
String beanClassPath = sonEle.attributeValue("class");
//3、拿到class地址,進行反射技術例項化物件,使用反射api為私有屬性賦值
Class<?> forName = Class.forName(beanClassPath);
obj = forName.newInstance();
//拿到成員屬性
List<Element> sonSoneleme = sonEle.elements();
for (Element element : sonSoneleme) {
String name = element.attributeValue("name");
String value = element.attributeValue("value");
//使用反射技術為私有屬性賦值
Field declaredField = forName.getDeclaredField(name);
//執行往私有屬性賦值
declaredField.setAccessible(true);
declaredField.set(obj, value);
}
}
return obj;
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, DocumentException {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("user.xml");
UserEntity user = (UserEntity) classPathXmlApplicationContext.getBean("user1");
System.out.println(user.getUserId() + "---" +user.getUserName());
}
}