在spring中如何注入一个java集合_Spring注入集合

您已经看到了如何配置基本数据类型使用value属性和使用标签的ref属性在你的bean配置文件中的对象引用。这两种情况下处理过单值到一个bean。

现在什么样,如果你想通过多元价值,如Java Collection类型List, Set, Map 及 Properties。要处理这种情况,Spring提供了四种类型的如下集合的配置元素:

元素

描述

这有助于注入值列表List配线,使重复。

这有助于在配线的一组值,但不能重复。

这可用于注入的名称 - 值对,其中名称和值可以是任何类型的集合。

这可以用来注入的名称 - 值对,其中名称和值都是字符串的集合。

可以使用 或 来连接任何实现java.util.Collection或数组。

会遇到两种情况(a)将收集的直接的值及(b)传递一个bean的引用作为集合的元素之一。

例子:

我们使用Eclipse IDE,然后按照下面的步骤来创建一个Spring应用程序:

步骤

描述

1

Create a project with a name SpringExample and create a package com.yiibai under the src folder in the created project.

2

Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter.

3

Create Java classes JavaCollection, and MainApp under the com.yiibai package.

4

Create Beans configuration file Beans.xml under the src folder.

5

The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below.

这里是JavaCollection.java文件的内容:

packagecom.yiibai;importjava.util.*;publicclassJavaCollection{ListaddressList;SetaddressSet;MapaddressMap;PropertiesaddressProp;// a setter method to set ListpublicvoidsetAddressList(ListaddressList){this.addressList=addressList;}// prints and returns all the elements of the list.publicListgetAddressList(){System.out.println("List Elements :"+addressList);returnaddressList;}// a setter method to set SetpublicvoidsetAddressSet(SetaddressSet){this.addressSet=addressSet;}// prints and returns all the elements of the Set.publicSetgetAddressSet(){System.out.println("Set Elements :"+addressSet);returnaddressSet;}// a setter method to set MappublicvoidsetAddressMap(MapaddressMap){this.addressMap=addressMap;}// prints and returns all the elements of the Map.publicMapgetAddressMap(){System.out.println("Map Elements :"+addressMap);returnaddressMap;}// a setter method to set PropertypublicvoidsetAddressProp(PropertiesaddressProp){this.addressProp=addressProp;}// prints and returns all the elements of the Property.publicPropertiesgetAddressProp(){System.out.println("Property Elements :"+addressProp);returnaddressProp;}}

以下是MainApp.java文件的内容:

packagecom.yiibai;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;publicclassMainApp{publicstaticvoidmain(String[]args){ApplicationContextcontext=newClassPathXmlApplicationContext("Beans.xml");JavaCollectionjc=(JavaCollection)context.getBean("javaCollection");jc.getAddressList();jc.getAddressSet();jc.getAddressMap();jc.getAddressProp();}}

以下是配置文件beans.xml文件里面有配置的集合的所有类型:

<?xml version="1.0"encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">INDIAPakistanUSAUSAINDIAPakistanUSAUSAINDIAPakistanUSAUSA

创建源代码和bean配置文件完成后,让我们运行应用程序。如果应用程序一切顺利,这将打印以下信息:

List Elements :[INDIA, Pakistan, USA, USA]

Set Elements :[INDIA, Pakistan, USA]

Map Elements :{1=INDIA, 2=Pakistan, 3=USA, 4=USA}

Property Elements :{two=Pakistan, one=INDIA, three=USA, four=USA}

注入Bean引用:

下面bean定义将帮助您了解如何注入bean的引用作为集合的元素之一。甚至可以混合引用和值都在一起,如下图所示:

<?xml version="1.0"encoding="UTF-8"?>

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">PakistanPakistan

使用上面的bean定义,需要定义这样一种方式,他们应该能够处理的参考,以及setter方法。

注入null和空字符串的值

如果需要传递一个空字符串作为值,如下所示:

前面的例子等同于Java代码: exampleBean.setEmail("")

如果需要传递一个null值,如下所示:

前面的例子等同于Java代码:exampleBean.setEmail(null)

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值