手写SpringIOC框架_反射技术+DOM4J解析xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 4 xsi:schemaLocation=" 5 http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 7 http://www.springframework.org/schema/tx 8 http://www.springframework.org/schema/aop 9 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 12 13 <bean id="user1" class="com.example.demo02.entity.User"> 14 <property name="userId" value="0003"></property> 15 <property name="userName" value="张三"></property> 16 </bean> 17 <bean id="user2" class="com.example.demo02.entity.User"> 18 <property name="userId" value="0004"></property> 19 <property name="userName" value="李四"></property> 20 </bean> 21 22 </beans>
1 package com.example.demo02.entity; 2 3 public class User { 4 private String userId; 5 private String userName; 6 7 public String getUserId() { 8 return userId; 9 } 10 11 public void setUserId(String userId) { 12 this.userId = userId; 13 } 14 15 public String getUserName() { 16 return userName; 17 } 18 19 public void setUserName(String userName) { 20 this.userName = userName; 21 } 22 23 @Override 24 public String toString() { 25 return "User{" + 26 "userId='" + userId + '\'' + 27 ", userName='" + userName + '\'' + 28 '}'; 29 } 30 }
1 package com.example.demo02.ioc; 2 3 import com.example.demo02.entity.User; 4 import org.dom4j.Document; 5 import org.dom4j.Element; 6 import org.dom4j.io.SAXReader; 7 import org.thymeleaf.util.StringUtils; 8 9 import java.lang.reflect.Field; 10 import java.util.List; 11 12 public class ClassPathXmlApplicationContext { 13 private String xmlPath; 14 15 public ClassPathXmlApplicationContext(String xmlPath) { 16 this.xmlPath = xmlPath; 17 } 18 19 public Object getBean(String beanId) throws Exception { 20 21 if (StringUtils.isEmpty(beanId)) { 22 throw new Exception("beanId is null"); 23 } 24 25 SAXReader saxReader = new SAXReader(); 26 Document document = saxReader.read(this.getClass().getClassLoader().getResource(xmlPath)); 27 Element rootElement = document.getRootElement(); 28 List<Element> elements = rootElement.elements(); 29 for (Element ele:elements) { 30 String id= ele.attributeValue("id"); 31 32 if (StringUtils.isEmpty(id)) { 33 continue; 34 } 35 if (!id.equals(beanId)) { 36 continue; 37 } 38 39 String beanClass = ele.attributeValue("class"); 40 Class<?> clazz = Class.forName(beanClass); 41 Object instance = clazz.newInstance(); 42 43 List<Element> elementList = ele.elements(); 44 for (Element element : elementList) { 45 String name = element.attributeValue("name"); 46 String value = element.attributeValue("value"); 47 Field declaredField = clazz.getDeclaredField(name); 48 declaredField.setAccessible(true); 49 declaredField.set(instance,value ); 50 } 51 return instance; 52 53 } 54 return null; 55 } 56 57 public static void main(String[] args) throws Exception { 58 ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); 59 User user1 = (User) applicationContext.getBean("user1"); 60 System.out.println(user1); 61 } 62 63 }