ioc 容器:
容器在本质上就是工厂(factory)
- IOC底层原理
- IOC 接口(BeanFactory)
- IOC操作Bean管理(基于xml)
- IOC 操作Bean管理(基于注解)
什么是IOC
ioc是控制反转,是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度,其中最常见的方式叫依赖注入还有一种方式是依赖查找。通过控制反转对象在被创建的时候没有一个调控系统内所有的对象的外界实体将所有依赖的对象引用传递给他,也可以说,依赖被注入到对象中
控制反转:把我们对象的创建和对象之间的条用的过程都交给Spring进行管理
目的 :为了降低耦合度,
IOC的底层原理
- xml的解析
- 工厂的设计模式
- 反射
ioc过程
第一步:xml配置文件,配置创建对象
<bean id="dao" class="com.Zhibin.UserDao"></bean>
第二部: 有service类 和dao 类, 创建工厂类
class UserFactory{
public static UserDao getDao(){
String classValue = class属性值;// 1.xml解析
Class clazz = Class.forName(classValue); // 3 通过反射创建对象
return (UserDao)class.newInstance();
}
}
IOC接口
- IOC 思想基于IOC容器完成,IOC 容器底层就是对象工厂
- Spring IOC容器的实现提供了两种方式: (两个重要的接口)
- BeanFactory: IOC容器基本实现, 是spring里边内部使用的接口,一般不提供开发人员进行使用
- 在使用BeanFactory进行加载xml文件的时候,不会创建对象,而我们在使用 或者获取对象的时候,才去创建对象
- ApplicationContext: 是Beanfactory的子接口,提供更强大的功能,一般有开发人员进行使用。
- 加载配置文件时就会配置文件对象进行创建
- BeanFactory: IOC容器基本实现, 是spring里边内部使用的接口,一般不提供开发人员进行使用
- ApplicationContext接口实现类
- 两个主要的实现类
- FileSystemXmlApplicationContext:系统盘中的xml
- ClassPathMxlApplicationContext : 类路径下的xml
- 两个主要的实现类
IOC操作Bean管理
-
什么是Bean管理
Bean管理值得是两个操作
- Spring创建对象
- Spring注入属性
-
Bean管理操作有两种方式
-
基于xml配置文件方式实现
-
基于xml创建属性
<!-- 配置User对象创建 在spring配置文件中 加上bean标签和对应的属性 bean标签的基本属性 id:给对象取的一个标识 class:类的全路径(包类路径) name:和id属性的作用差不多,但是可以加一些特殊符号 创建对象的时候也是默认的创建其相应的无参构造方法; 当没有无参构造方法时会报错: Caused by: java.lang.NoSuchMethodException: com.Zhibin.spring5.User.<init>() --> <bean id="user" class="com.Zhibin.spring5.User" name=""></bean>
-
基于xml方式注入属性
-
DI(IOC的一种具体实现): 依赖注入,就是注入属性
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"> <!-- 配置user 类对象的创建 --> <bean id="user" class="com.Zhibin.spring5.User"> <!-- 使用property 完胜属性注入 --> <property name="userName" value="zhibin"></property> <property name="gender" value="male"></property> <property name="email" value="zhibin@.com"></property> <!--设置空值的方式 <property name="email" value="zhibin@.com"> <null/> </property> --> <!-- 属性中包含特殊符号 1 把<> 进行转义 <:<<南京>>> 2 把带特殊符号内容写道CDATA <property name="address"> <value><![CDATA[<<南京>>]]</value> </property> 效果为 <<南京>> --> </bean> </beans>
test
@Test public void testAdd(){ // 记载Spring 的配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); // 获取配置创建对象 User user = context.getBean("user",User.class); System.out.println(user);//User{userName='zhibin', gender='male', email='zhibin@.com'} ; }
使用有参构造注入属性
<!-- 使用有参构造注入属性 --> <bean id="orders" class="com.Zhibin.spring5.Olders" > <!-- 也可以是 index="0" value="computer" 就是使用索引 第一个变量用0 第二个为1 以此类推 --> <constructor-arg name="oName" value="computer"></constructor-arg> <constructor-arg name="address" value="China"></constructor-arg> </bean>
xml 属性注入方式之 引入外部Bean(通过service层来调用dao层)
-
创建service 和 dao
-
在service 调用dao 的方法
<bean id="userService" class="com.Zhibin.spring5.service.UserService"> <!-- id = “类里面的属性值” ref= 相应类的bean --> <property name="userDao" ref="userDao"></property> </bean> <bean id="userDao" class="com.Zhibin.spring5.dao.UserDaoImpl"></bean>
xml属性注入值 内部bean
-
一对多关系: 部门和员工
-
一个部门多个员工,一个员工属于一个部门
-
部门为一, 员工为多
<bean id="Emp" class="com.Zhibin.spring5.bean.Emp"> <!-- --> <property name="eName" value="lucy"> </property> <property name="gender" value="female"> </property> <property name="dept"> <bean id="Dept" class="com.Zhibin.spring5.bean.Dept" > <property name="deptName" value="安保部门"></property> </bean> </property> </bean> <!-- 对于嵌套的类型直接在property里面写一个bean -->
级联复制
-
和外部bean类似
-
级联
<bean id="Emp" class="com.Zhibin.spring5.bean.Emp"> <!-- --> <property name="eName" value="lucy"> </property> <property name="gender" value="female"> </property> <property name="dept" ref="Dept" > </property> <property name="dept.deptName" value="软件开发"></property> </bean> <bean id="Dept" class="com.Zhibin.spring5.bean.Dept"> <!-- 对于相应的内容可以直接从内部定义 使用name.属性 这里的 "." 作用就相当于get方法,所以在使用之前确保有相应的get方法 -->
注入集合类型的属性
-
注入数组类型的属性
-
注入List 类型的属性
-
注入Map类型的属性
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"> <!-- --> <bean id="Stu" class="com.Zhibin.spring5.collectionType.Stu"> <!-- 数组注入 --> <property name="courses" > <array> <value>java</value> <value>c#</value> <value>python</value> </array> </property> <property name="list"><!--list--> <list> <value>张三</value> <value>小三</value> </list> </property> <property name="map" ><!--map--> <map> <entry key="c语言" value="84"></entry> </map> </property> <property name="sets"><!--set类型--> <set> <value>mysql</value> <value>redis</value> </set> </property> <!-- 注入List 集合的值 值为对象 --> <property name="courseList"> <list> <ref bean="course1"></ref> <ref bean="course2"></ref> </list> </property> </bean> <bean id="course1" class="com.Zhibin.spring5.collectionType.Course"> <property name="cname" value="javaSE" ></property> </bean> <bean id="course2" class="com.Zhibin.spring5.collectionType.Course"> <property name="cname" value="Spring"></property> </bean> </beans>
-
-
-
-
基于注解方式实现
-