您的位置:首页 > 编程语言 > Java开发

spring(DI)概念、xml方式给属性赋值

2016-02-09 18:32 627 查看
概念

给属性赋值

**// XML: 方式给属性赋值**


<!--
把person放入到spring容器中
-->
<bean id="person" class="com.itheima09.spring.di.xml.setter.Person"
init-method="init"
lazy-init="true">
<!--
property 用来描述person类中的属性
name属性 代表属性的名称
value属性 代表属性的值  属性为基本类型
因为student是引用类型,所以用ref赋值
-->
<property name="pid" value="2"></property>
<property name="name" value="王二麻子"></property>

<property name="student" ref="student"></property>

//list
<property name="list">
<list>
<value>list1</value>
<value>list2</value>
<ref bean="student"/>
</list>
</property>

//set
<property name="set">
<set>
<value>set1</value>
<value>set2</value>
<ref bean="student"/>
</set>
</property>

//map
<property name="map">
<map>
<entry key="entry1">
<value>entry1</value>
</entry>
<entry key="entry2">
<ref bean="student"/>
</entry>
</map>
</property>

//properties
<property name="properties">
<props>
<prop key="prop1">prop1</prop>
<prop key="prop2">prop2</prop>
</props>
</property>

</bean>
<!--
把student放入到spring容器中
-->
<bean id="student"
class="com.itheima09.spring.di.xml.setter.Student"></bean>




说明:

1、spring容器实例化person和student两个对象

2、利用java的反射机制调用属性的setter方法赋值

3、在客户端利用context.getBean方法把spring容器中的一个对象获取了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: