1、Spring.xml
XML<beans> <bean id = "orderController" class="com.atguigu.controller.OrderController"> <property name = "orderService" ref = "orderService"/> </bean> <bean id = "orderService" class="com.atguigu.service.impl.OrderServiceImpl"> <property name="orderDao" ref="orderDao"/> </bean> <bean id = "orderDao" class="com.atguigu.dao.impl.OrderDaoImpl"></bean> </beans>
2、ApplicationContext.java
javapackage com.atguigu.ioc; import java.util.HashMap; import java.util.Map; public interface ApplicationContext { Map<String,Object> beanMap = new HashMap<>(); public void registryBean(String beanName,Object component); public Map<String,Object> getIocContainer(); Object getBean(String beanName); }
3、ClassPathXmlApplicationContext.java
javapackage com.atguigu.ioc; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.io.BufferedInputStream; import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class ClassPathXmlApplicationContext implements ApplicationContext{ private static final String BEAN_LABEL = "bean"; private static final String ATTR_ID_LABEL = "id"; private static final String ATTR_CLAZZ_LABEL = "class"; private static final String PROPERTY_LABEL = "property"; private static final String PROPERTY_NAME_LABEL = "name"; private static final String PROPERTY_REF_LABEL = "ref"; /** * beans * bean bean bean */ public ClassPathXmlApplicationContext(String resolvedFilePath) { SAXReader saxReader = new SAXReader(); InputStream is = this.getClass().getClassLoader().getResourceAsStream(resolvedFilePath); try { Document document = saxReader.read(new BufferedInputStream(is)); Element rootElement = document.getRootElement(); List<Element> beanList = rootElement.elements().stream().filter(element -> element.getName().compareTo(BEAN_LABEL) == 0).collect(Collectors.toList()); if (beanList == null || beanList.size() == 0){ return; } beanList.forEach(beanElement -> { Attribute idAttr = beanElement.attribute(ATTR_ID_LABEL); Attribute clazzAttr = beanElement.attribute(ATTR_CLAZZ_LABEL); String id = idAttr.getData().toString(); String clazzName = clazzAttr.getData().toString(); try { Object beanInstance = Class.forName(clazzName).getDeclaredConstructor().newInstance(); registryBean(id,beanInstance); } catch (InstantiationException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } }); beanList.forEach(beanElement -> { String componentClazzName = beanElement.attributeValue(ATTR_CLAZZ_LABEL); String componentId = beanElement.attributeValue(ATTR_ID_LABEL); try { Class<?> componentClazz = Class.forName(componentClazzName); List<Element> propertyList = beanElement.elements().stream().filter(element -> element.getName().compareTo(PROPERTY_LABEL) == 0).collect(Collectors.toList()); propertyList.stream().forEach(property -> { String name = property.attributeValue(PROPERTY_NAME_LABEL); String ref = property.attributeValue(PROPERTY_REF_LABEL); try { Field targetInvokeField = componentClazz.getDeclaredField(name); targetInvokeField.setAccessible(true); targetInvokeField.set(getBean(componentId),getBean(ref)); } catch (NoSuchFieldException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }); } catch (Exception e) { throw new RuntimeException(e); } }); } catch (DocumentException e) { throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); } System.out.println(beanMap); } @Override public void registryBean(String beanName, Object component) { beanMap.put(beanName,component); } @Override public Map<String, Object> getIocContainer() { return beanMap; } @Override public Object getBean(String beanName) { return beanMap.get(beanName); } }
4、OrderDao.java
javapackage com.atguigu.dao; public interface OrderDao { void start(); }
4.1、OrderDaoImpl.java
javapackage com.atguigu.dao.impl; import com.atguigu.dao.OrderDao; public class OrderDaoImpl implements OrderDao { @Override public void start() { System.out.println("orderDao start..."); } }
5、OrderService.java
javapackage com.atguigu.service; public interface OrderService { void progress(); }
5.1、OrderServiceImpl.java
javapackage com.atguigu.service.impl; import com.atguigu.dao.OrderDao; import com.atguigu.service.OrderService; public class OrderServiceImpl implements OrderService { private OrderDao orderDao; @Override public void progress() { System.out.println("orderService start..."); orderDao.start(); } }
6、OrderController
javapackage com.atguigu.controller; import com.atguigu.service.OrderService; public class OrderController { private OrderService orderService; public void save(){ System.out.println("orderController start..."); orderService.progress(); } }
7、Client
javapackage com.atguigu.client; import com.atguigu.controller.OrderController; import com.atguigu.ioc.ApplicationContext; import com.atguigu.ioc.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring.xml"); OrderController orderController = (OrderController) applicationContext.getBean("orderController"); orderController.save(); } }
8、pom.xml
XML<dependencies> <dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artifactId> <version>2.1.4</version> </dependency> </dependencies>
可以使用以下代码来查看 IOC 容器中有哪些 bean 对象:
javaApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); String[] beans = context.getBeanDefinitionNames(); for(String bean: beans) { System.out.println(bean); }
其中,
getBeanDefinitionNames()
方法返回一个字符串数组,里面存放了所有在 IOC 容器中注册的 bean 对象的名称。遍历输出这个数组就能够查看当前 IOC 容器中有哪些 bean 对象。
模拟Spring源码思想,读取Spring Config配置文件,创建IOC容器,解析spring.xml,创建对象,放入IOC容器,注入依赖
丁总学Java2023-11-25 18:57
相关推荐
Yan-英杰3 分钟前
DeepSeek-R1模型现已登录亚马逊云科技TDengine (老段)14 分钟前
TDengine 中的日志系统不想学习!!15 分钟前
linux之进程控制nlog3n1 小时前
Java外观模式详解Mryan20051 小时前
SpringBoot项目报错: 缺少 ValidationSnXJi_2 小时前
开源赋能,双驱协同:纷析云财务与进销存软件助力企业数字化转型eternal__day2 小时前
第三期:深入理解 Spring Web MVC [特殊字符](数据传参+ 特殊字符处理 + 编码问题解析)iccb10132 小时前
在线聊天系统中的多窗口数据同步技术解密逸狼3 小时前
【JavaEE进阶】MyBatis(5)-MyBatis-plusAphelios3803 小时前
Java全栈面试宝典:线程协作与Spring Bean管理深度解析