- Spring初学快速入门
- 安装Spring工具套件到Eclipse
- Spring快速入门
- Spring自动装配Bean
- Spring JDBC支持
- Spring JavaConfig
- Spring 依赖注入(DI)
-
Spring Bean基础
- Spring Bean引用例子
- 如何注入值到Spring bean属性
- Spring bean加载多个配置文件
- Spring内部bean实例
- Spring Bean作用域实例
- Spring集合 (List,Set,Map,Properties) 实例
- Spring ListFactoryBean实例
- Spring SetFactoryBean实例
- Spring MapFactoryBean例子
- Spring注入日期到bean属性-CustomDateEditor
- Spring PropertyPlaceholderConfigurer实例
- Spring bean配置继承
- Spring依赖检查
- Spring使用@Required注解依赖检查
- Spring自定义@Required-style注解
- Spring Bean InitializingBean和DisposableBean实例
- Spring Bean init-method 和 destroy-method实例
- Spring @PostConstruct和@PreDestroy实例
- Spring表达式语言
- Spring自动组件扫描
- Spring AOP (面向方面编程)
- Spring AOP + AspectJ框架
- Spring Hibernate支持
- Spring E-mail支持
-
Spring与其它Web框架集成
Spring集合 (List,Set,Map,Properties) 实例
下面例子向您展示Spring如何注入值到集合类型(List, Set, Map, and Properties)。 支持4个主要的集合类型:
- List – <list/>
- Set – <set/>
- Map – <map/>
- Properties – <props/>
Spring beans
一个Customer对象,有四个集合属性。
package com.zyiz.netmon; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Customer { private List<Object> lists; private Set<Object> sets; private Map<Object, Object> maps; private Properties pros; //... }
在bean配置文件中不同的代码片段用来声明集合。
1. List示例
<property name="lists"> <list> <value>1</value> <ref bean="PersonBean" /> <bean class="com.zyiz.netmon.Person"> <property name="name" value="zyizList" /> <property name="address" value="Hainan" /> <property name="age" value="28" /> </bean> </list> </property>
2. Set示例
<property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.zyiz.netmon.Person"> <property name="name" value="zyizSet" /> <property name="address" value="Hainan" /> <property name="age" value="28" /> </bean> </set> </property>
3. Map示例
<property name="maps"> <map> <entry key="Key 1" value="1" /> <entry key="Key 2" value-ref="PersonBean" /> <entry key="Key 3"> <bean class="com.zyiz.netmon.Person"> <property name="name" value="zyizMap" /> <property name="address" value="Hainan" /> <property name="age" value="28" /> </bean> </entry> </map> </property>
4. Properties示例
<property name="pros"> <props> <prop key="admin">admin@zyiz.net</prop> <prop key="support">support@zyiz.net</prop> </props> </property>
Spring完整的 bean 配置文件。
<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-2.5.xsd"> <bean id="CustomerBean" class="com.zyiz.netmon.Customer"> <!-- java.util.List --> <property name="lists"> <list> <value>1</value> <ref bean="PersonBean" /> <bean class="com.zyiz.netmon.Person"> <property name="name" value="zyizList" /> <property name="address" value="Hainan Haikou" /> <property name="age" value="28" /> </bean> </list> </property> <!-- java.util.Set --> <property name="sets"> <set> <value>1</value> <ref bean="PersonBean" /> <bean class="com.zyiz.netmon.Person"> <property name="name" value="zyizSet" /> <property name="address" value="Hainan Haikou" /> <property name="age" value="28" /> </bean> </set> </property> <!-- java.util.Map --> <property name="maps"> <map> <entry key="Key 1" value="1" /> <entry key="Key 2" value-ref="PersonBean" /> <entry key="Key 3"> <bean class="com.zyiz.netmon.Person"> <property name="name" value="zyizMap" /> <property name="address" value="Hainan Haikou" /> <property name="age" value="28" /> </bean> </entry> </map> </property> <!-- java.util.Properties --> <property name="pros"> <props> <prop key="admin">admin@zyiz.net</prop> <prop key="support">support@zyiz.net</prop> </props> </property> </bean> <bean id="PersonBean" class="com.zyiz.netmon.Person"> <property name="name" value="zyiz1" /> <property name="address" value="Hainan Haikou 1" /> <property name="age" value="28" /> </bean> </beans>
执行程序
package com.zyiz.netmon; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Customer cust = (Customer)context.getBean("CustomerBean"); System.out.println(cust); } }
输出
Customer [lists=[1, com.zyiz.netmon.Person@4e4ee70b, com.zyiz.netmon.Person@1e1867d2], sets=[1, com.zyiz.netmon.Person@4e4ee70b, com.zyiz.netmon.Person@52f644b4], maps={Key 1=1, Key 2=com.zyiz.netmon.Person@4e4ee70b, Key 3=com.zyiz.netmon.Person@54481b6d}, pros={admin=admin@zyiz.net, support=support@zyiz.net}]
分类导航
- Java教程
- Vim教程
- Swing教程
- Spring教程
- Spring Web Services教程
- Spring MVC教程
- Spring JDBC教程
- Spring Cloud教程
- Spring Boot教程
- Spring Boot CLI教程
- Spring Batch教程
- Spring AOP教程
- PDFBox教程
- JSP教程
- JSF教程
- JPA教程
- Java面向对象设计
- Java设计模式
- Java虚拟机教程
- Java泛型教程
- Java正则表达式教程
- Java数据类型教程
- Java并发编程教程
- Java密码学教程
- Java多线程教程
- Java国际化(i18n)教程
- JavaFX教程
- Java9教程
关注微信小程序
扫描二维码
程序员编程王