IOC底层原理解析

IOC完成处理,需要经过一下处理步骤:

1.XML解析技术读取配置文件

spring会将将下面的信息读取进入程序  对象的ID ,一个是对象的类的全路径名

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="empService" class="com.augus.service.impl.EmpServiceImpl"/>
</beans>

2.产生对象放入容器

通过反射技术产生实例化对象,放到容器中

//获得类的字节码
Class clazz =Class.forName("com.augus.service.impl.EmpServiceImpl")
//通过字节码实例化对象
Object obj  = clazz.newInstance();
//将对象放到一个map集合中
map.put("empService",obj)

3.通过工厂模式返回Bean对象 getBean方法

public Object getBean(String name){
            Object obj =map.get(name);
            return obj;
}   

IOC接口

  • BeanFactory 接口: IOC容器基本功能接口,是spring内部使用的接口,我们在处理业务时一般不直接使用该接口
  • ApplicationContext 接口: BeanFactory的子接口,提供更多更强大的功能,研发人员一般使用的接口
 
posted @ 2019-10-22 11:59  酒剑仙*  阅读(838)  评论(0编辑  收藏  举报