-
通过配置
bean
标签来完成对象的管理。-
id
:对象名。 -
class
:对象的模板类。(所有交给IoC容器来管理的类必须有无参构造函数,因为Spring底层是通过反射机制来创建对象,调用的是无参构造)
-
-
对象的成员变量通过
property
标签完成赋值- name : 成员变量名。
- value :成员变量值 (基本数据类型 ,String可以直接赋值,如果是其他引用类型,不能通过value赋值)
ref
:将IoC中的另一个bean赋给当前的成员变量(DI)
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context">
<bean id="student" class="com.southwind.entity.Student">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="22"></property>
<property name="address" ref="address"></property>
</bean>
<bean id="address" class="com.southwind.entity.Address">
<property name="id" value="1"></property>
<property name="name" value="科技路"></property>
</bean>
</beans>