Spring中的集合的注入方式
2016-01-08 00:00
645 查看
摘要: 在Spring中可以装配4种集合类型属性:List、set、Map和Properties。介绍如何装配集合属性,还有一个数组
上代码 bean.xml的配置
上面是实体类User,下面是需要注入的实体类
测试类
测试结果
上代码 bean.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="collectionBean" class="org.heinrich.CollectionBean"> <property name="sets"> <set> <value>Heinrich</value> <value>Reading Thinking Java</value> </set> </property> <property name="users"> <list> <ref bean="user1"/> <ref bean="user2"/> </list> </property> <property name="maps"> <map> <entry key="BookName"> <value>Thinking Java</value> </entry> <entry key="PhoneName"> <value>Apple 6 plus</value> </entry> </map> </property> <property name="properties"> <props> <prop key="1">MySQL</prop> <prop key="2">Oracle</prop> <prop key="3">SQLSERVER</prop> </props> </property> </bean> <bean id="user1" class="org.heinrich.User"> <property name="name"> <value>Heinrich</value> </property> <property name="age"> <value>21</value> </property> </bean> <bean id="user2" class="org.heinrich.User"> <property name="name"> <value>Zhangyanmei</value> </property> <property name="age"> <value>22</value> </property> </bean> </beans>
package org.heinrich; public class User { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
上面是实体类User,下面是需要注入的实体类
package org.heinrich; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class CollectionBean { private List<User> users; private Map<String,String> maps; private Set<String> sets; private Properties properties; public List<User> getUsers() { return users; } public void setUsers(List<User> users) { this.users = users; } public Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public Set<String> getSets() { return sets; } public void setSets(Set<String> sets) { this.sets = sets; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } }
测试类
package org.heinrich; import org.springframework.context.support.ClassPathXmlApplicationContext; public class BeanTest { public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); User user1 = (User)ctx.getBean("user1"); User user2 = (User)ctx.getBean("user1"); System.out.println(user1); System.out.println(user2); CollectionBean collectionBean = (CollectionBean)ctx.getBean("collectionBean"); System.out.println(collectionBean); System.out.println(collectionBean.getMaps()); System.out.println(collectionBean.getSets()); System.out.println(collectionBean.getUsers()); System.out.println(collectionBean.getProperties()); } }
测试结果
org.heinrich.User@f8dd88b org.heinrich.User@f8dd88b org.heinrich.CollectionBean@298395a7 {BookName=Thinking Java, PhoneName=Apple 6 plus} [Heinrich, Reading Thinking Java] [org.heinrich.User@f8dd88b, org.heinrich.User@7dd61c3b] {3=SQLSERVER, 2=Oracle, 1=MySQL}
相关文章推荐
- java设计模式——单例模式
- Head First Java 阅读笔记
- java文件cmd窗口编译后,运行class文件时报找不到类的错误。
- java文件cmd窗口编译后,运行class文件时报找不到类的错误。
- JAVA生成持久化流水号
- Spring框架工作原理及其优缺点详解
- JAVA CAS原理深度分析
- [java] java 线程join方法详解
- springmvc 声明响应内容的格式和编码
- Java简单爬虫系列(4)--- 正则表达式获取百度LOGO
- springmvc 多文件上传接收方式
- JAVA正则表达式语法大全
- java多线程简易爬虫
- HeadFirst Java
- Java Day9
- This application requires Java Runtime Environment
- LeetCode – Two Sum (Java)
- Spring 4 MVC入门实例
- [转]java构造方法的访问修饰符
- 简化Spring的XML配置(一)——自动装配bean属性