Spring学习(三)几种集合属性的注入方式
2024-10-11 16:44:01
1、前言
众所周知、java中不只有八大简单类型、还有一些集合类型、本文围绕集合类型的注入做一个总结。
2、项目骨架
3、过程
1、创建实体类AllCollectionType
package com.feng.entity;
import java.util.*;
public class AllCollectionType {
private List<String> listElement;
private String[] arrayElement;
private Set<String> setElement;
private Map<String,String> mapElement;
private Properties propsElement;
@Override
public String toString() {
return "AllCollectionType{" +
"listElement=" + listElement +
", arrayElement=" + Arrays.toString(arrayElement) +
", setElement=" + setElement +
", mapElement=" + mapElement +
", propsElement=" + propsElement +
'}';
}
public List<String> getListElement() {
return listElement;
}
public void setListElement(List<String> listElement) {
this.listElement = listElement;
}
public String[] getArrayElement() {
return arrayElement;
}
public void setArrayElement(String[] arrayElement) {
this.arrayElement = arrayElement;
}
public Set<String> getSetElement() {
return setElement;
}
public void setSetElement(Set<String> setElement) {
this.setElement = setElement;
}
public Map<String, String> getMapElement() {
return mapElement;
}
public void setMapElement(Map<String, String> mapElement) {
this.mapElement = mapElement;
}
public Properties getPropsElement() {
return propsElement;
}
public void setPropsElement(Properties propsElement) {
this.propsElement = propsElement;
}
}
2、配置applicationContext.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="collectionDemo" class="com.feng.entity.AllCollectionType">
<property name="listElement">
<list>
<value>足球</value>
<value>蓝球</value>
<value>乒乓球</value>
</list>
</property>
<property name="arrayElement">
<array>
<value>足球1</value>
<value>蓝球1</value>
<value>乒乓球1</value>
</array>
</property>
<property name="setElement">
<set>
<value>足球2</value>
<value>蓝球2</value>
<value>乒乓球2</value>
</set>
</property>
<property name="mapElement">
<map>
<entry>
<key>
<value>foot3</value>
</key>
<value>足球3</value>
</entry>
<entry>
<key>
<value>basket3</value>
</key>
<value>蓝球3</value>
</entry>
<entry>
<key>
<value>pp3</value>
</key>
<value>乒乓球3</value>
</entry>
</map>
</property>
<property name="propsElement">
<props>
<prop key="foot4">足球4</prop>
<prop key="basket4">蓝球4</prop>
<prop key="pp4">乒乓球4</prop>
</props>
</property>
</bean>
</beans>
3、编写测试类
package com.feng.test;
import com.feng.entity.AllCollectionType;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void collectionDemo() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
AllCollectionType type = (AllCollectionType)context.getBean("collectionDemo");
System.out.println(type);
}
public static void main(String[] args) {
collectionDemo();
}
}
4、运行结果
4、总结
本文简单讲述了四种集合属性的注入方式、对之前的注入方式进行了一次补充。
最新文章
- MVC判断用是否登录了平台
- 知乎日报win10版 - 天天读报【开源】
- git常用操作
- JVM的类加载机制
- 关于for循环条件性能问题
- lintcode:Search Insert Position 搜索插入位置
- ECSHOP - 二次开发指南---购物车篇
- 设置VS2015上关闭和打开tab快捷键
- BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏( floyd )
- DOM简要
- selenium-webdriver(python) 11
- css3文本字体
- 爬取字段和图片 spider_getModelInformation
- js 依据“;”折行
- xilinx_all_version.lic
- for循环,列表,元组
- .NET 构造DataTable返回多个json值
- 跟我一起使用electron搭建一个文件浏览器应用吧(二)
- 雷林鹏分享:XML 命名空间
- [BZOJ2616]SPOJ PERIODNI 树形dp+组合数+逆元