一些其他ioc管理的bean属性
<bean id="studentOne" class="com.tian.spring.pojo.Student"></bean>
<bean id="studentTwo" class="com.tian.spring.pojo.Student">
<property name="sid" value="1001"></property>
<property name="sname" value="张三"></property>
<property name="age" value="23"></property>
<property name="gender" value="男"></property>
</bean>
<bean id="studentThree" class="com.tian.spring.pojo.Student">
<constructor-arg value="1002"></constructor-arg>
<constructor-arg value="李四"></constructor-arg>
<constructor-arg value="女"></constructor-arg>
<constructor-arg value="80" name="age"></constructor-arg>
</bean>
<bean id="studentFour" class="com.tian.spring.pojo.Student">
<property name="sid" value="1003"></property>
<!--
<:<
>:>
-->
<!-- <property name="sname" value="<王五>"></property>-->
<property name="sname">
<value><![CDATA[<王五>]]></value>
</property>
<property name="gender">
<null/>
</property>
</bean>
<bean id="studentFive" class="com.tian.spring.pojo.Student">
<property name="sid" value="1004"></property>
<property name="sname" value="赵六"></property>
<property name="age" value="26"></property>
<property name="gender" value="男"></property>
<property name="clazz">
<!--内部bean,只能在当前bean的内部使用,不能直接通过IOC容器获取-->
<bean id="clazzInner" class="com.tian.spring.pojo.Clazz">
<property name="cid" value="2222"></property>
<property name="cname" value="远大前程班"></property>
</bean>
</property>
<property name="hobby">
<array>
<value>唱</value>
<value>跳</value>
<value>rap</value>
</array>
</property>
一:List集合
1:通过List标签来进行赋值
<bean id="clazzOne" class="com.tian.spring.pojo.Clazz">
<property name="cid" value="1111"></property>
<property name="cname" value="最强王者班"></property>
<property name="students">
<list>
<ref bean="studentOne"></ref>
<ref bean="studentTwo"></ref>
<ref bean="studentThree"></ref>
</list>
</property>
</bean>
2:通过配置一个List集合类型的bean,但是需要用到util的一个约束
<!--配置一个集合类型的bean,需要使用util的约束-->
<util:list id="studentList">
<ref bean="studentOne"></ref>
<ref bean="studentTwo"></ref>
<ref bean="studentThree"></ref>
</util:list>
<bean id="clazzOne" class="com.tian.spring.pojo.Clazz">
<property name="cid" value="1111"></property>
<property name="cname" value="最强王者班"></property>
<property name="students" ref="studentList"></property>
</bean>
二:Map集合
1:通过map标签来进行赋值
<bean id="teacherOne" class="com.tian.spring.pojo.Teacher">
<property name="tid" value="10086"></property>
<property name="tname" value="大宝"></property>
</bean>
<bean id="teacherTwo" class="com.tian.spring.pojo.Teacher">
<property name="tid" value="10010"></property>
<property name="tname" value="小宝"></property>
</bean>
<property name="teacherMap">
<map>
<entry key="10086" value-ref="teacherOne"></entry>
<entry key="10010" value-ref="teacherTwo"></entry>
</map>
</property>
2:通过配置一个Map集合类型的bean,但是需要用到util的一个约束
<util:map id="teacherMap">
<entry key="10086" value-ref="teacherOne"></entry>
<entry key="10010" value-ref="teacherTwo"></entry>
</util:map>
<property name="teacherMap" ref="teacherMap"></property>
三:p命名空间
<bean id = "studentSix" class="com.tian.spring.pojo.Student"
p:sid="1005" p:sname="小明" p:teacherMap-ref="teacherMap">
</bean>
没带ref的是给字面量类型赋值的,带ref是给类类型属性去引用某一个bean的id的
@Test
public void testDI(){
//获取IOC容器
ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-ioc.xml");
//获取bean
Student student = ioc.getBean("studentSix", Student.class);
System.out.println(student);
}