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
相关推荐
驭渊的小故事35 分钟前
MyBatis XML 开发实战:从配置到 CRUD 全解析哦虎!5 小时前
【JavaEE】网络初识for_ever_love__6 小时前
MySQL ORDER BY 排序优化:从 Using filesort 到索引有序扫描(含双路 / 单路排序原理)我是人✓7 小时前
Java文件上传三方式对比:从传统到SpringBoot欣栀♡7 小时前
JVM 频繁 Full GC 先看日志括号里的 cause,jstat 的 FGC 在 JDK 8 上会骗人IT研究室7 小时前
最新计算机毕业设计选题推荐-基于spring boot的葡萄酒文化宣传网站-网站-文档指导-Java-springboot慧都小项7 小时前
MyEclipse 中创建和测试 JAX-RS REST 接口:从项目配置到 REST ExplorerWang's Blog7 小时前
Java 项目实战: 交友项目 - 入门案例:编写 Dubbo 服务提供者秋饼8 小时前
Spring AI 2.0 结构化输出企业级实战:从 BeanOutputConverter 源码到生产级解析容错摇滚侠8 小时前
《计算机是如何工作的:人人都能懂的计算机软硬件工作原理》第 9 章 机器码与汇编语言 阅读笔记 9