spring-注入集合对象

本文内容纲要:spring-注入集合对象

1.创建Stu类

package com.spring.collections;

import java.util.Arrays;

import java.util.List;

import java.util.Map;

import java.util.Set;

public class Stu {

//1.数组类型

private String[] courses;

//2.list集合属性

private List<String> list;

//3.map集合类型

private Map<String,String> map;

//4.set集合属性类型

private Set<String> set;

public void setSet(Set<String> set) {

this.set = set;

}

public void setMap(Map<String, String> map) {

this.map = map;

}

public void setList(List<String> list) {

this.list = list;

}

public void setCourses(String[] courses) {

this.courses = courses;

}

public void test(){

System.out.println(Arrays.toString(courses));

System.out.println(list);

System.out.println(map);

System.out.println(set);

}

}

2.bean1.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 name="stu" class="com.spring.collections.Stu">

<!--数组类型的注入-->

<property name="courses">

<array> <!--array表示数组注入形式-->

<value>Struts框架</value>

<value>Spring框架</value>

</array>

</property>

<!--list类型的注入-->

<property name="list">

<list>

<value>mybatis框架</value>

<value>Spring MVC</value>

</list>

</property>

<!--map类型的注入-->

<property name="map">

<map>

<entry key="JAVA" value="java"></entry>

<entry key="SSM" value="ssm"></entry>

</map>

</property>

<!--set类型注入-->

<property name="set">

<set>

<value>kevin</value>

<value>Ron</value>

</set>

</property>

</bean>

</beans>

3.创建测试类进行测试

package com.spring.test;

import com.spring.collections.Stu;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpringDemo1 {

@Test

public void testCollection(){

//加载bean1.xml文件

ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean1.xml");

//获取配置对象

Stu stu = applicationContext.getBean("stu", Stu.class);

stu.test();//调用方法

}

}

本文内容总结:spring-注入集合对象

原文链接:https://www.cnblogs.com/kevinsvim/p/14614983.html

以上是 spring-注入集合对象 的全部内容, 来源链接: utcz.com/z/296008.html

回到顶部