代碼示例以下:spring
建立BeanClass實體類數組
public class BeanClass { private String[] arrs;//數組類型 private List<String> list;//List集合類型 private Set<String> set;//Set集合類型 private Map<String,Integer> map;//Map集合類型 private Properties prop;//屬性類型 //getter和setter方法 //toString()方法 }
在applicationContext.xml中配置Beanapp
<?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.xsd"> <!--集合類型屬性注入--> <bean id="beanClass" class="entity.BeanClass"> <!--數組類型--> <property name="arrs"> <list> <value>aa</value> <value>bb</value> </list> </property> <!--List集合--> <property name="list"> <list> <value>111</value> <value>222</value> </list> </property> <!--Set集合--> <property name="set"> <set> <value>aaa</value> <value>bbb</value> </set> </property> <!--Map集合--> <property name="map"> <map> <entry key="aa" value="11"></entry> <entry key="bb" value="22"></entry> </map> </property> <!--Properties屬性--> <property name="prop"> <props> <prop key="aa">11</prop> <prop key="bb">22</prop> </props> </property> </bean> </beans>
測試代碼測試
@Test public void demo(){ //初始化Spring容器ApplicationContext,加載配置文件 ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //經過容器獲取實例 BeanClass beanClass = (BeanClass) ctx.getBean("beanClass"); System.out.println(beanClass); }
運行結果spa