Spring如何装配各种集合类型的属性
来源:互联网 发布:加盟淘宝网店是真的吗 编辑:程序博客网 时间:2024/06/08 11:51
在前面我们已经会注入基本类型对象和其他bean,现在我们就来学习如何注入各种集合类型。
Spring如何装配各种集合类型的属性
首先新建一个普通的Java Project,名称为spring_collection,并迅速搭建好Spring的开发环境。
接着在src目录下新建一个cn.itcast.service包,并在该包下创建PersonService接口,其代码为:
public interface PersonService { Set<String> getSets(); List<String> getLists(); Properties getProperties(); Map<String, String> getMaps(); void save();}
再接下来仍在src目录下新建一个cn.itcast.service.impl包,并在该包下创建PersonService接口的实现类——PersonServiceBean.java,其代码为:
public class PersonServiceBean implements PersonService { private Set<String> sets = new HashSet<String>(); private List<String> lists = new ArrayList<String>(); private Properties properties = new Properties(); private Map<String, String> maps = new HashMap<String, String>(); public Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } public List<String> getLists() { return lists; } public void setLists(List<String> lists) { this.lists = lists; } public Set<String> getSets() { return sets; } public void setSets(Set<String> sets) { this.sets = sets; } @Override public void save() { }}
然后将Spring的配置文件——beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"> <property name="sets"> <set> <value>第一个</value> <value>第二个</value> <value>第三个</value> </set> </property> <property name="lists"> <list> <value>第一个list元素</value> <value>第二个list元素</value> <value>第三个list元素</value> </list> </property> <property name="properties"> <props> <prop key="key1">value1</prop> <prop key="key2">value2</prop> <prop key="key3">value3</prop> </props> </property> <property name="maps"> <map> <entry key="key-1" value="value-1"></entry> <entry key="key-2" value="value-2"></entry> <entry key="key-3" value="value-3"></entry> </map> </property> </bean></beans>
最后,在src目录下新建一个junit.test包,并在该包下新建一个单元测试类——SpringTest.java,其代码为:
public class SpringTest { @Test public void instanceSpring() { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService"); System.out.println("===================set==================="); for (String value : personService.getSets()) { System.out.println(value); } System.out.println("===================list==================="); for (String value : personService.getLists()) { System.out.println(value); } System.out.println("===================properties==================="); for (Object key : personService.getProperties().keySet()) { System.out.println(key + "=" + personService.getProperties().getProperty((String) key)); } System.out.println("===================maps==================="); for (Object key : personService.getMaps().keySet()) { System.out.println(key + "=" + personService.getMaps().get(key)); } ctx.close(); }}
测试instanceSpring()方法,会发现Eclipse的控制台打印:
如要查看源码,可点击Spring如何装配各种集合类型的属性进行下载。
使用构造器装配属性
前面我们就已讲过spring的依赖注入有两种方式:
- 使用构造器注入。
- 使用属性setter方法注入。
我们已经详解过使用属性setter方法注入这种方式,接下来自然就到了使用构造器注入属性了。
首先将PersonService接口的代码改为:
public interface PersonService { void save();}
接着将PersonServiceBean实现类的代码修改为:
public class PersonServiceBean implements PersonService { private PersonDao personDao; private String name; public PersonServiceBean(PersonDao personDao, String name) { this.personDao = personDao; this.name = name; } @Override public void save() { System.out.println(name); personDao.add(); }}
然后将Spring的配置文件修改为:
<?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="personDao" class="cn.itcast.dao.impl.PersonDaoBean" /> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"> <constructor-arg index="0" type="cn.itcast.dao.PersonDao" ref="personDao" /> <constructor-arg index="1" value="李阿昀" /> <!-- <property name="sets"> <set> <value>第一个</value> <value>第二个</value> <value>第三个</value> </set> </property> <property name="lists"> <list> <value>第一个list元素</value> <value>第二个list元素</value> <value>第三个list元素</value> </list> </property> <property name="properties"> <props> <prop key="key1">value1</prop> <prop key="key2">value2</prop> <prop key="key3">value3</prop> </props> </property> <property name="maps"> <map> <entry key="key-1" value="value-1"></entry> <entry key="key-2" value="value-2"></entry> <entry key="key-3" value="value-3"></entry> </map> </property> --> </bean></beans>
最后,将单元测试类——SpringTest.java的代码改为:
public class SpringTest { @Test public void instanceSpring() { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService"); personService.save(); ctx.close(); }}
测试instanceSpring()方法,可看到Eclipse控制台打印:
0 0
- Spring如何装配各种集合类型的属性
- Spring如何装配各种集合类型的属性
- Spring如何装配各种集合类型的属性
- Spring如何装配各种集合类属性
- Spring装配各种集合类型的配置文件
- Spring学习笔记(7)----装配各种集合类型的属性
- Spring学习笔记(7)----装配各种集合类型的属性
- 09_传智播客Spring2.5视频教程_Spring如何装配各种集合类型的属性
- Spring学习笔记——Spring如何装配各种类型的属性以及实际应用
- Spring学习笔记04--spring如何装配集合类型的属性
- spring装配基本属性和集合类型
- Spring装配集合属性
- spring集合类型装配
- Spring-集合类型装配
- spring对于集合类型的装配
- [Spring学习笔记 2 ]装配各种类型的属性 map,list,array,null,properties
- Spring2.5学习2.4_Spring如何装配各种集合类的属性
- spring的集合装配
- Xcode:Playground中延时调用的新写法
- 互联网广告系统综述三业务描述
- Raspberry Pi3 安装 ROS桌面完整版攻略
- 常用的日期格式化工具类
- python 基础代码注释和时间属性
- Spring如何装配各种集合类型的属性
- RecyclerView监听
- JavaScript创建对象几种方式
- Python 基于python+Testlink+Jenkins实现的接口自动化测试框架
- 简单实用的linux分区办法
- LeetCode 382. Linked List Random Node
- JavaBean的映射工具: Dozer
- Android阶段性学习总结
- 图片剪裁cropper插件结合网站示例 支持图片旋转,裁剪,加水印功能 类似58同城效果