spring 框架 list、set、map、Properties数组注入

关于spring框架中数组的注入

1、先声明一个java类()声明list、set、map等属性集合,

package com.zxk.entity;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class SystemIoc {
//声明各种数组变量
private String[] strings;//string类型数组
private Usb[] usbs;//对象类型数组
private List lists1;
private List<Integer> lists2;
private List<Usb> lists3;
private Set<Usb> sets;
private Map<String, Usb> maps;
private Properties pros;
public String[] getStrings() {
return strings;
}
public void setStrings(String[] strings) {
this.strings = strings;
}
public Usb[] getUsbs() {
return usbs;
}
public void setUsbs(Usb[] usbs) {
this.usbs = usbs;
}
public List getLists1() {
return lists1;
}
public void setLists1(List lists1) {
this.lists1 = lists1;
}
public List<Integer> getLists2() {
return lists2;
}
public void setLists2(List<Integer> lists2) {
this.lists2 = lists2;
}
public List<Usb> getLists3() {
return lists3;
}
public void setLists3(List<Usb> lists3) {
this.lists3 = lists3;
}
public Set<Usb> getSets() {
return sets;
}
public void setSets(Set<Usb> sets) {
this.sets = sets;
}
public Map<String, Usb> getMaps() {
return maps;
}
public void setMaps(Map<String, Usb> maps) {
this.maps = maps;
}
public Properties getPros() {
return pros;
}
public void setPros(Properties pros) {
this.pros = pros;
}


}

再编写一个对象类用于注入

package com.zxk.entity;
public class Usb{
private Integer size;

public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
}

2、编写applicationContext.xml配置文件

<beans>
<bean id="usb1" class="com.zxk.entity.Usb">
<property name="size" value="1000"></property>
</bean>
<bean id="usb2" class="com.zxk.entity.Usb">
<property name="size" value="2000"></property>
</bean>
<!-- 集合注入 -->
<bean id="systemIoc" class="com.zxk.entity.SystemIoc">
<!-- 基本数据类型的数组注入 -->
<property name="strings">
<list>
<value>zhangsan</value>
<value>lisi</value>
<value>wangwu</value>
</list>
</property>
<!-- 对象类型的数组注入 -->
<property name="usbs">
<list>
<ref bean="usb1"/>
<ref bean="usb2"/>
<bean class="com.zxk.entity.Usb">
<property name="size" value="3000"></property>
</bean>
</list>
</property>
<!-- 不带泛型的List集合注入 -->
<property name="lists1">
<list>
<value>zhangsan</value>
<value>18</value>
<ref bean="usb1"/>
<bean class="java.util.Date"></bean>
</list>
</property>
<!-- 带基本数据类型泛型的List集合注入 -->
<property name="lists2">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<!-- 带对象类型泛型的List集合注入 -->
<property name="lists3">
<list>
<ref bean="usb1"/>
<ref bean="usb1"/>
<bean class="com.zxk.entity.Usb">
<property name="size" value="3000"></property>
</bean>
</list>
</property>
<!-- 带对象类型泛型的Set集合注入 -->
<property name="sets">
<set>
<ref bean="usb1"/>
<ref bean="usb2"/>
<bean class="com.zxk.entity.Usb">
<property name="size" value="3000"></property>
</bean>
</set>
</property>
<!-- 带对象类型泛型的Map集合注入 -->
<property name="maps">
<map>
<entry key="key1"  value-ref="usb1"></entry>
<entry key="key2" value-ref="usb2"></entry>
<entry key="key3">
<bean class="com.zxk.entity.Usb">
<property name="size" value="3000"></property>
</bean>
</entry>
</map>
</property>
<!-- Properties集合注入 -->
<property name="pros">
<props>
<prop key="pro1">
配置1
</prop>
<prop key="pro2">
配置2
</prop>
<prop key="pro3">
配置3
</prop>
</props>
</property>
</bean>
</beans>

3、编写测试类

package com.zxk.test;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zxk.entity.SystemIoc;
import com.zxk.entity.Usb;

public class Test {
public static void main(String[] args) {
//读取spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取表示的对象
SystemIoc systemIoc = (SystemIoc) context.getBean("systemIoc");
//输出内容
System.out.println("---------基本类型数组输出--------------");
String[] strings = systemIoc.getStrings();
for (String string : strings) {
System.out.println(string);
}

System.out.println("---------对象类型数组输出--------------");

Usb[] usbs = systemIoc.getUsbs();
for (Usb usb : usbs) {
System.out.println(usb.getSize());
}

System.out.println("---------list无参类型数组输出--------------");

List list1 = systemIoc.getLists1();
System.out.println(list1.get(2));

System.out.println("---------list泛型类型数组输出--------------");

List<Integer> list2 = systemIoc.getLists2();
for (Integer integer : list2) {
System.out.println(integer);
}

System.out.println("---------list对象类型数组输出--------------");

List<Usb> list3 = systemIoc.getLists3();
for (Usb usb : list3) {
System.out.println(usb.getSize());
}

System.out.println("---------set对象类型数组输出--------------");

Set<Usb> sets = systemIoc.getSets();
for (Usb usb : sets) {
System.out.println(usb.getSize());
}

System.out.println("---------Map数组输出--------------");

Map<String,Usb> maps = systemIoc.getMaps();
for (Usb usb : maps.values()) {
System.out.println(usb.getSize());
}

System.out.println("---------Properties数组输出--------------");

Properties pros = systemIoc.getPros();
for (Object str : pros.values()) {
System.out.println(str);
}
}
}

运行上述代码最后输出结果:


扫描二维码关注公众号,回复: 3563694 查看本文章


温馨提示:复制以上代码,运行即可出结果


注:转载请声明出处

猜你喜欢

转载自blog.csdn.net/qq_40083897/article/details/79545882