spring的两大思想,IOC和AOP,IOC实现对象的创建和生命周期管理,AOP面向切面编程,将和业务逻辑没有直接关联的业务代码从主业务代码中抽离出来,需要的时候再切入进去,实现程序的可插拔和弱耦合.
spring根据一个配置文件就能帮助我们创建对象,并注入属性.于是乎,就自己写个程序去解析xml,根据里面的配置,往类里面的属性注入值,最终实现的效果是这样的:

Step1:新建一个配置文件applicationContext.xml,当然不叫这个名也是可以的.

Step2:创建两个实体,Person和Dog

public class Dog {private String type;private String color;@Overridepublic String toString() {return "Dog [type=" + type + ", color=" + color + "]";}public void setType(String type) {this.type = type;}public void setColor(String color) {this.color = color;}
}
public class Person {private String name;private Integer age;private Dog dog;public void setName(String name) {this.name = name;}public void setAge(Integer age) {this.age = age;}public void setDog(Dog dog) {this.dog = dog;}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + ", dog=" + dog + "]";}}

重写toString()方法,目的是让打印结果更直接;
Step3:核心程序,解析xml,往两个实体注入属性,廉颇老将军不仅能吃饭还左牵黄右擎苍~

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.util.Objects;
import javax.xml.parsers.DocumentBuilderFactory;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;public class SpringMockTest {@Testpublic void testSpringMock() throws Exception {// 加载xml文件String path = Objects.requireNonNull(SpringMockTest.class.getClassLoader().getResource("applicationContext.xml")).getPath();File file = new File(path);// 解析文件成DOMDocument document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);// 获取根元素Element rootEle = document.getDocumentElement();// 获取所有的bean元素对象NodeList beans = rootEle.getElementsByTagName("bean");Element personBeanEle = (Element) beans.item(0);// 获取person元素的属性值NodeList PersonBeanProperties = personBeanEle.getElementsByTagName("property");Element personBeanPropEle1 = (Element) PersonBeanProperties.item(0);Element personBeanPropEle2 = (Element) PersonBeanProperties.item(1);Element personBeanPropEle3 = (Element) PersonBeanProperties.item(2);// 获取Person的字节码对象Class<?> personClz = Class.forName(personBeanEle.getAttribute("class"));Object person = personClz.newInstance();// 获取Person的属性描述器PropertyDescriptor[] personPds = Introspector.getBeanInfo(personClz, Object.class).getPropertyDescriptors();//运用内省机制注入属性值for (PropertyDescriptor personPd : personPds) {if (personPd.getName().equals(personBeanPropEle1.getAttribute("name"))) {personPd.getWriteMethod().invoke(person, personBeanPropEle1.getAttribute("value"));} else if (personPd.getName().equals(personBeanPropEle2.getAttribute("name"))) {personPd.getWriteMethod().invoke(person, Integer.parseInt(personBeanPropEle2.getAttribute("value")));} else {Object dog = null;for (int i = 0; i < beans.getLength(); i++) {Element beanEle = (Element) beans.item(i);if (personBeanPropEle3.getAttribute("ref").equals(beanEle.getAttribute("id"))) {//通过反射获得dog对象Class<?> dogClz = Class.forName(beanEle.getAttribute("class"));dog = dogClz.newInstance();//获取dog对象的属性节点列表NodeList dogBeanProperties = beanEle.getElementsByTagName("property");//获取dog对象属性描述器PropertyDescriptor[] dogPds = Introspector.getBeanInfo(dogClz, Object.class).getPropertyDescriptors();for (PropertyDescriptor dogPd : dogPds) {for (int j = 0; j < dogBeanProperties.getLength(); j++) {Element dogBeanPropEle = (Element)dogBeanProperties.item(j);if (dogPd.getName().equals(dogBeanPropEle.getAttribute("name"))) {//如果xml中属性名和dog对象属性名一致,则注入值dogPd.getWriteMethod().invoke(dog, dogBeanPropEle.getAttribute("value"));}}}}}//同理,person对象注入相应属性值personPd.getWriteMethod().invoke(person, dog);}}System.out.println(person);}}

到此就结束了,代码很简单,主要是理解一遍spring IOC思想.
spring对bean进行实例化,默认bean是单例.
通过查看源码我发现spring实现了很多的BeanPostProcessor,来实现对象生命周期的管理,有张图可以参考下:

spring如何实现IOC和DI思想?相关推荐

  1. 用IDEA详解Spring中的IoC和DI(挺透彻的,点进来看看吧)

    用IDEA详解Spring中的IoC和DI 一.Spring IoC的基本概念 控制反转(IoC)是一个比较抽象的概念,它主要用来消减计算机程序的耦合问题,是Spring框架的核心. 依赖注入(DI) ...

  2. Spring详解—— IOC 和 DI 区别

    1.本章前言 我们只要提到Spring这个词,有简单了解过Spring的人基本上都会脱口而出IoC.DI和AOP这几个概念.但是对于初学者来说,一下子搞懂IoC和DI的概念还是挺麻烦的.比如之前我自己 ...

  3. Spring框架,IOC,DI,AOP,单例多例,懒加载

    文章目录 1.Spring 2.IOC 2.1 什么是IOC 2.2 xml配置文件管理对象 2.3 全注解的方式管理对象 3.Spring创建对象-工厂模式(必会内容) 4.单例与多例 4.1@Sc ...

  4. 请简述什么是spring的ioc和di_理解Spring中的IoC和DI

    什么是IoC和DI IoC(Inversion of Control 控制反转):是一种面向对象编程中的一种设计原则,用来减低计算机代码之间的耦合度.其基本思想是:借助于"第三方" ...

  5. 请简述什么是spring的ioc和di_小编Spring是什么、spring容器、IOC和DI

    1.Spring (1)Spring是什么? 是一个轻量级的.用来简化企业级应用开发的开发框架. 注: a.简化开发: Spring对常用的api做了简化,比如,使用Spring jdbc来访问数据库 ...

  6. Spring中的Ioc、DI到底是啥

    我们都知道,Spring是一款轻量级的IOC框架,Spring的核心就是Ioc和DI,并通过俩者解耦. 那么,Ioc.DI呢? Ioc 控制反转 Ioc(Inversion of control)控制 ...

  7. Spring详细导包截图以及IOC和DI思想

    思想 IOC DI Spring中的工厂(容器) ApplicationContext: BeanFactory(过时): BeanFactory和ApplicationContext区别 BeanF ...

  8. 【Spring学习】IoC、DI、AOP入门学习

    新建一个普通的java项目,添加spring功能(3.x),不要使用MyEclipse提供的jar包,自己手动添加需要的jar包(commons-logging.jar.spring.jar): Io ...

  9. Spring框架中IOC和DI的区别

    IOC:全称Inversion of Control,中文释义为控制反转. 简单地说,IOC就是把对象的实例化工作交由Spring容器来完成.IOC可以说是一种思想,传统的应用程序是我们自己去创建(n ...

  10. 谈谈Spring中的IOC、DI和AOP概念

    看了大神的解释感觉受益匪浅,所以就将其保存,方便自己看,并最后总结出自己的理解 1. IOC(Inverse of Control):控制反转,也可以称为依赖倒置. 所谓依赖,从程序的角度看,就是比如 ...

最新文章

  1. 王者荣耀活动精选 Blink 第二弹来袭!
  2. 【原创】gooogleman亲自参与设计的三星Cortex A8 S5pv210 之Sate210核心板硬件用户手册(作者:gooogleman)...
  3. 【原创】开源Math.NET基础数学类库使用(06)直接求解线性方程组
  4. 链家混三个月底薪_链家悲惨工作经历,新人参考
  5. 作为一名产品经理,我是如何快速做项目计划的?
  6. 如何更改Windows 10锁屏界面超时时间
  7. Java生产力提示:社区的热门选择
  8. python-主成分分析-降维-PCA
  9. mysql面试题分组并合并列
  10. yarn报错:error An unexpected error occurred: “https://registry.yarnpkg.com/-/user/org.couchdb。。。
  11. 为什么div设置其border无效?
  12. 1526B - I Hate 1111
  13. 快速搭建一个直播Demo
  14. html文字居中单词,html文字居中
  15. 新买的固态硬盘写保护,无法格式化,怎么破!
  16. 单片机/开发板连接配置的三种方式
  17. 关于SES2000找管线定位问题的实验
  18. byte为什么是-128-127
  19. message sent to deallocated instance 0x154eec600
  20. 图像平滑之二维离散卷积

热门文章

  1. 上传图片转为base64码再以url形式传值
  2. Selenium 定位Loading元素图标,只需要简单配置下就行
  3. python如何调用pyd_C#调用pyd的方法
  4. 6-2 求解一元二次方程实根的函数 (10 分)
  5. 博士论文-基于生成对抗网络的图像合成-阅读笔记
  6. 服务器两块硬盘怎么合并,电脑高手进(怎么把俩块硬盘合并在一同)
  7. Windows初级运维(一)文件查找DOS命令大全
  8. 广域网加速方案--Riverbed
  9. 手机怎么识别图中文字?这两个方法靠谱
  10. OBS开源免费桌面视频直播工具/直播推流工具使用指南