IoC配置文件
- 通过配置bean标签来完成对象的管理
- id:对象名
- class:对象的模板类(所有交给IoC容器来管理的类必须有无参构造函数,因为Spring底层通过反射机制来创建对象,调用的是无参构造)
- 对象的成员变量通过property标签来赋值
- name:成员变量名
- value:成员变量值(基本数据类型,string可以直接赋值,其他引用类型不可以通过value来赋值)
- ref:将IoC的另外一个bean赋值给当前的成员变量
<bean id="student" class="com.lalila.entity.entity.Student">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
<property name="address" ref="address"></property>
</bean>
<bean id="address" class="com.lalila.entity.entity.Address">
<property name="id" value="1"></property>
<property name="name" value="科技路"></property>
</bean>
IoC底层原理
- 读取配置文件,解析xml
- 通过反射机制来实例化配置文件中所配置的所有的bean
将多个bean注入一个bean
<bean id="student" class="com.lalila.entity.entity.Student">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
<property name="addresses" >
<list>
<ref bean="address"></ref>
<ref bean="address2"></ref>
</list>
</property>
</bean>
<bean id="address" class="com.lalila.entity.entity.Address">
<property name="id" value="1"></property>
<property name="name" value="科技路"></property>
</bean>
<bean id="address2" class="com.lalila.entity.entity.Address">
<property name="id" value="2"></property>
<property name="name" value="高新区"></property>
</bean>
scope作用域
Spring管理的bean是根据scope来生成的,表示bean的作用域
- singleton:单例,表示Spring容器获得的bean是唯一的
- prototype:原型,表示Spring容器获得的bean是不同的
- request:请求,表示再一次HTTP请求中有效
- session:会话,表示在一次用户会话中有效
request和session只试用于web项目
Spring继承
和Java中的继承不同,Java是类层面的继承,子类继承父类的内容结构信息。Spring是对象层面的继承,子对象可以继承父对象的属性值。
<bean id="student" class="com.lalila.entity.entity.Student">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
<property name="addresses" >
<list>
<ref bean="address"></ref>
<ref bean="address2"></ref>
</list>
</property>
</bean>
<bean id="stu" class="com.lalila.entity.entity.Student" parent="student"></bean>
要覆盖父对象,则可以直接在property标签中写。不同对象之间也可以继承,但是子对象必须包含父对象的所有内容。
Spring的依赖
和继承类似,也是描述bean和bean之间的一种关系。配置依赖之后,被依赖的bean一定是先创建,然后再创建依赖的bean。A依赖于B,则先创建B,再创建A。
<bean id="student" class="com.lalila.entity.entity.Student" depends-on="user"></bean>
<bean id="user" class="com.lalila.entity.entity.User"></bean>
student依赖user,所以先创建user,再创建student
需要设定两个bean的加载顺序的时候使用依赖
Spring的p命名空间
p命名空间是对IoC/DI的简化,可以更加方便地完成bean地配置和依赖注入
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
<bean id="student" class="com.lalila.entity.entity.Student" p:id="1" p:name="张三" p:age="22" p:address-ref="address"></bean>
<bean id="address" class="com.lalila.entity.entity.Address" p:id="2" p:name="张三"></bean>
</beans>
Spring的工厂方法
IoC通过工厂模式创建bean的方式有两种
- 静态工厂方式
- 实例工厂方式
IoC自动装载(Autowire)
Ioc负责创建对象,DI负责完成对象的依赖注入,通过配置property标签的ref属性来完成。同时Spring提供了一种更加简便的依赖注入方式:自动装载,不需要手动配置property。Ioc容器会自动选择bean完成注入。
两种方式:
- byName
- byType
如果使用byType,有两个相同需要类型的bean,则会报错