Spring注入集合

简介:

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

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

元素 描述
<list> 这有助于注入值列表List配线,使重复。
<set> 这有助于在配线的一组值,但不能重复。
<map> 这可用于注入的名称 – 值对,其中名称和值可以是任何类型的集合。
<props> 这可以用来注入的名称 – 值对,其中名称和值都是字符串的集合。

可以使用<list> 或<set> 来连接任何实现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文件的内容:

 
package com . yiibai ;
import java . util .*;
public class JavaCollection {
List addressList ;
Set addressSet ;
Map addressMap ;
Properties addressProp ;
// a setter method to set List
public void setAddressList ( List addressList ) {
this . addressList = addressList ;
}
// prints and returns all the elements of the list.
public List getAddressList () {
System . out . println ( "List Elements :" + addressList );
return addressList ;
}
// a setter method to set Set
public void setAddressSet ( Set addressSet ) {
this . addressSet = addressSet ;
}
// prints and returns all the elements of the Set.
public Set getAddressSet () {
System . out . println ( "Set Elements :" + addressSet );
return addressSet ;
}
// a setter method to set Map
public void setAddressMap ( Map addressMap ) {
this . addressMap = addressMap ;
}
// prints and returns all the elements of the Map.
public Map getAddressMap () {
System . out . println ( "Map Elements :" + addressMap );
return addressMap ;
}
// a setter method to set Property
public void setAddressProp ( Properties addressProp ) {
this . addressProp = addressProp ;
}
// prints and returns all the elements of the Property.
public Properties getAddressProp () {
System . out . println ( "Property Elements :" + addressProp );
return addressProp ;
}
}

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

 
package com . yiibai ;
import org . springframework . context . ApplicationContext ;
import org . springframework . context . support . ClassPathXmlApplicationContext ;
public class MainApp {
public static void main ( String [] args ) {
ApplicationContext context = new ClassPathXmlApplicationContext ( "Beans.xml" );
JavaCollection jc =( JavaCollection ) context . getBean ( "javaCollection" );
jc . getAddressList ();
jc . getAddressSet ();
jc . getAddressMap ();
jc . getAddressProp ();
}
}

以下是配置文件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-3.0.xsd" >
<!-- Definition for javaCollection -->
<bean id = "javaCollection" class = "com.yiibai.JavaCollection" >
<!-- results in a setAddressList(java.util.List) call -->
<property name = "addressList" >
<list>
<value> INDIA </value>
<value> Pakistan </value>
<value> USA </value>
<value> USA </value>
</list>
</property>

<!-- results in a setAddressSet(java.util.Set) call -->
<property name = "addressSet" >
<set>
<value> INDIA </value>
<value> Pakistan </value>
<value> USA </value>
<value> USA </value>
</set>
</property>

<!-- results in a setAddressMap(java.util.Map) call -->
<property name = "addressMap" >
<map>
<entry key = "1" value = "INDIA" />
<entry key = "2" value = "Pakistan" />
<entry key = "3" value = "USA" />
<entry key = "4" value = "USA" />
</map>
</property>

<!-- results in a setAddressProp(java.util.Properties) call -->
<property name = "addressProp" >
<props>
<prop key = "one" > INDIA </prop>
<prop key = "two" > Pakistan </prop>
<prop key = "three" > USA </prop>
<prop key = "four" > USA </prop>
</props>
</property>
</bean>
</beans>

创建源代码和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" ?>
<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-3.0.xsd" >
<!-- Bean Definition to handle references and values -->
<bean id = "..." class = "..." >
<!-- Passing bean reference for java.util.List -->
<property name = "addressList" >
<list>
<ref bean = "address1" />
<ref bean = "address2" />
<value> Pakistan </value>
</list>
</property>
<!-- Passing bean reference for java.util.Set -->
<property name = "addressSet" >
<set>
<ref bean = "address1" />
<ref bean = "address2" />
<value> Pakistan </value>
</set>
</property>
<!-- Passing bean reference for java.util.Map -->
<property name = "addressMap" >
<map>
<entry key = "one" value = "INDIA" />
<entry key = "two" value-ref = "address1" />
<entry key = "three" value-ref = "address2" />
</map>
</property>
</bean>
</beans>

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

注入null和空字符串的值

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

 
<bean id = "..." class = "exampleBean" >
<property name = "email" value = "" />
</bean>

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

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

 
<bean id = "..." class = "exampleBean" >
<property name = "email" >
<null/>
</property>
</bean>

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


原文发布时间为:2018-10-22

本文作者:小白教程

本文来自云栖社区合作伙伴“Java杂记”,了解相关信息可以关注“Java杂记”。

相关文章
|
1月前
|
XML Java 开发者
Spring Boot中的bean注入方式和原理
Spring Boot中的bean注入方式和原理
52 0
|
3月前
|
Java 程序员 Spring
Spring5深入浅出篇:Spring对象属性注入详解
Spring5深入浅出篇:Spring对象属性注入详解
|
4月前
|
Java 开发者 Spring
Spring中获取Bean对象的三种注入方式和两种注入方法
Spring中获取Bean对象的三种注入方式和两种注入方法
|
12天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
21 1
|
1月前
|
XML Java 数据格式
解释Spring中一个bean的注入过程
解释Spring中一个bean的注入过程
|
2月前
|
Java Spring
Spring 使用注解注入失败,调用内容时提示空指针
Spring 使用注解注入失败,调用内容时提示空指针
14 0
|
2月前
|
Java Spring
Spring注入类的两种形式
Spring注入类的两种形式
49 0
Spring注入类的两种形式
|
3月前
|
XML 缓存 Java
Spring5源码(7)-lookup-method和replace-method注入
Spring5源码(7)-lookup-method和replace-method注入
19 0
|
3月前
|
XML Java 数据格式
Spring5源码(6)-Spring注入集合属性
Spring5源码(6)-Spring注入集合属性
14 0
|
3月前
|
Java Spring 容器
面试题:Spring框架中,一个组件是怎样向自己内部注入组件的
面试题:Spring框架中,一个组件是怎样向自己内部注入组件的
14 0