09_传智播客Spring2.5视频教程_Spring如何装配各种集合类型的属性

本文介绍了一个使用Spring框架进行集合类型属性注入的例子,包括Set、List、Properties及Map等类型的注入方式,并通过JUnit测试验证了配置的有效性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?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-2.5.xsd">
 <bean id="personService"
  class="nine.spring.service.impl.PersonServiceBean">
  <property name="sets">
   <set>
    <value>我</value>
    <value>你</value>
    <value>他</value>
   </set>
        </property>
        <property name="lists">
            <list>
                <value>2</value>
                <value>3</value>
                <value>4</value>
            </list>
  </property>
        <property name="properties">
            <props>
            <prop key="key1">w</prop>
            <prop key="key2">e</prop>
            <prop key="key3">w</prop>
            </props>
        </property>
        <property name="maps">
            <map>
              <entry key="key1" value="a"/>
              <entry key="key1" value="s"/>
              <entry key="key3" value="d"/>
            </map>
        </property>
 </bean>
</beans>

package nine.spring.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import nine.spring.service.PersonService;

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>();
   省略 get set 方法

 

 

package nine.spring.junit;

import java.util.Properties;
import java.util.Set;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import nine.spring.service.PersonService;

public class SpringTest {

 @BeforeClass
 public static void setUpBeforeClass() throws Exception {
 }
                                                                           
 @Test
 public void instanceSpring() {

  // 在类路径下,寻找配置文件,实例化spring容器。
  ApplicationContext ctx = new ClassPathXmlApplicationContext("/nine/ninebeans.xml");

  //配置文件中bean的id,推荐首字母小写
  PersonService personService = (PersonService) ctx.getBean("personService");

  for (String value : personService.getSets()) {
   System.out.print(value);
  }
  for (String value : personService.getLists()) {
   System.out.print(value);
  }
  for (Object value : personService.getProperties().keySet()) {
   System.out.print(personService.getProperties().getProperty((String)value));
  }
  for (String value : personService.getMaps().keySet()) {
   System.out.print(personService.getMaps().get((String)value));
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值