Spring如何装配各种集合类型的属性

在前面我们已经会注入基本类型对象和其他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的依赖注入有两种方式:

  1. 使用构造器注入。
  2. 使用属性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控制台打印:
这里写图片描述

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李阿昀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值