1、嵌套Bean:
<bean id="newPerson" class="com.sxit.service.NewPerson" > <property name="chinese"> <bean class="com.sxit.service.newChinese"/> </property> </bean>
2、嵌套Bean特性:
1)、嵌套Bean只对嵌套在它外面Bean有效,容器无法访问到它,嵌套Bean无需指定id。
3、值的注入:
1)、属性如果是基本类型,String,日期等,直接使用value指定即可 2)、属性如果是复合类,需要传递一个Java对象作为实参,有两种方式:一种是通过ref引用容器中已配置的Bean,第二种是配置一个嵌套Bean。
4、Bean的属性是集合,如何注入:
package com.sxit.service; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Chinese{ private List<String> schools; private Map scores; private Map<String,Person> noPerson; private Properties health; private Set person; private String[] books; public Chinese(){ System.out.println("Spring容器实例化Chinese..."); } public void info(){ System.out.println(schools); System.out.println(scores); System.out.println(noPerson); System.out.println(health); System.out.println(person); System.out.println(Arrays.toString(books).toString()); } //省略get set .... }
5、配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <bean id="chinese" class="com.sxit.service.Chinese"> <property name="schools"> <list> <value>小学</value> <value>中学</value> <value>大学</value> </list> </property> <property name="scores"> <map> <entry key="语文" value="99"/> <entry key="数学" value="19"/> <entry key="英语" value="29"/> </map> </property> <property name="books"> <list> <value>小学数学</value> <value>初中化学</value> <value>大学物理</value> </list> </property> <property name="health"> <props> <prop key="身高">180</prop> <prop key="姓名">傻逼</prop> <prop key="年龄">18</prop> </props> </property> <property name="noPerson"> <map> <entry key="中国人" value-ref="chinesePerson"/> <entry key="美国人" value-ref="usPerson"/> <entry key="欧洲人" value-ref="ouPerson"/> </map> </property> <property name="person"> <set> <value>普通人</value> <bean class="com.sxit.service.ChinesePerson"> <property name="type" value="嵌套的中国人"/> </bean> </set> </property> </bean> <bean id="chinesePerson" class="com.sxit.service.ChinesePerson"> <property name="type" value="中国人"/> </bean> <bean id="usPerson" class="com.sxit.service.usPerson"> <property name="type" value="美国人"/> </bean> <bean id="ouPerson" class="com.sxit.service.ouPerson"> <property name="type" value="欧洲人"/> </bean> </beans>
6、测试类及打印信息:
package com.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.sxit.service.Chinese; public class Test { public static void main(String[] args) { ApplicationContext apc = new ClassPathXmlApplicationContext("Bean.xml"); Chinese chinese = apc.getBean("chinese",Chinese.class); chinese.info(); } } Spring容器实例化Chinese... [小学, 中学, 大学] {语文=99, 数学=19, 英语=29} {中国人=com.sxit.service.ChinesePerson@aa37a6, 美国人=com.sxit.service.usPerson@12b7eea, 欧洲人=com.sxit.service.ouPerson@99353f} {姓名=傻逼, 身高=180, 年龄=18} [普通人, com.sxit.service.ChinesePerson@4b035d] [小学数学, 初中化学, 大学物理]