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
相关推荐
张某布响丸辣6 分钟前
SQL中的时间类型:深入解析与应用喜欢打篮球的普通人12 分钟前
rust模式和匹配java小吕布25 分钟前
Java中的排序算法:探索与比较慢生活的人。31 分钟前
SpringSecurity+jwt+captcha登录认证授权总结2401_857600951 小时前
深入剖析:Spring MVC与Struts的较量被猫枕的咸鱼1 小时前
springmvc通过使用map来进行数据的接收和使用向阳12181 小时前
LeetCode40:组合总和II云空1 小时前
《InsCode AI IDE:编程新时代的引领者》慧都小妮子1 小时前
Spire.PDF for .NET【页面设置】演示:复制 PDF 文档中的页面Dr_eamboat1 小时前
【Java】枚举类映射