栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

1.4.2.4. Collections(集合)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

1.4.2.4. Collections(集合)

 Spring framework documentation (5.3.10)
Core

IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP.

   Core Technologies

1. The IoC Container

1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)

1.2. Container Overview (容器概览)

1.3. Bean Overview (Bean概览)

1.4. Dependencies(依赖)

1.4.1. Dependency Injection(依赖注入)

1.4.2. Dependencies and Configuration in Detail(依赖与配置详细介绍)

1.4.2.1. Straight Values (Primitives, Strings, and so on)(直接值(原始类型、字符串等))

1.4.2.2. References to Other Beans (Collaborators) (引用其他bean(协作者))

1.4.2.3. Inner Beans(内部bean)

1.4.2.4. Collections(集合)

1.4.2.5. Null and Empty String Values(Null和空字符串值)

1.4.2.6. XML Shortcut with the p-namespace(p命名空间的XML快捷方式)

1.4.2.7. XML Shortcut with the c-namespace(c-namespace的XML快捷方式)

1.4.2.8. Compound Property Names(复合属性名)

1.4.3. Using depends-on(使用depends-on)

1.4.4. Lazy-initialized Beans(延迟初始化Bean)

1.4.5. Autowiring Collaborators(自动装配协作者)

1.4.6. Method Injection(方法注入)

1.5. Bean Scopes(Bean作用域)


关于Spring framework documentation (5.3.10)  核心技术的更多内容,请点击:

  Core Technologies


1.4.2.4. Collections(集合)

The , and  elements set the properties and arguments of the Java Collection types List, Set, Map, and Properties, respectively. The following example shows how to use them:

元素分别设置Java集合类型 List, Set, Map和Properties的属性和参数。以下示例展示了如何使用它们:


    
    
        
            administrator@example.org
            support@example.org
            development@example.org
        
    
    
    
        
            a list element followed by a reference
            
        
    
    
    
        
            
            
        
    
    
    
        
            just some string
            
        
    

The value of a map key or value, or a set value, can also be any of the following elements:

map的键或值的值,或集合值的值也可以是以下任意元素:

bean | ref | idref | list | set | map | props | value | null

Collection Merging (集合合并)

The Spring container also supports merging collections. An application developer can define a parent  or  element and have child  or  elements inherit and override values from the parent collection. That is, the child collection’s values are the result of merging the elements of the parent and child collections, with the child’s collection elements overriding values specified in the parent collection.

Spring容器还支持合并集合(merging collection)。应用程序开发人员可以定义父级元素,并让子级元素继承和重写父集合中的值。也就是说,子集合的值是合并父集合和子集合的元素的结果,子集合的元素覆盖父集合中指定的值。

This section on merging discusses the parent-child bean mechanism. Readers unfamiliar with parent and child bean definitions may wish to read the relevant section before continuing.

本节是关于合并的,同时讨论了父子bean机制。不熟悉父bean和子bean定义的读者可能希望在继续之前阅读相关部分( relevant section )。

The following example demonstrates collection merging:

以下示例演示了集合合并(ollection merging):


    
        
            
                administrator@example.com
                support@example.com
            
        
    
    
        
            
            
                sales@example.com
                support@example.co.uk
            
        
    

Notice the use of the merge=true attribute on the  element of the adminEmails property of the child bean definition. When the child bean is resolved and instantiated by the container, the resulting instance has an adminEmails Properties collection that contains the result of merging the child’s adminEmails collection with the parent’s adminEmails collection. The following listing shows the result:

注意在child bean定义的adminEmails 属性的元素上使用了merge=true属性。当容器解析并实例化child bean时,生成的实例具有一个adminEmails 属性集合,该集合包含将child bean的adminEmails集合与parent bean的adminEmails集合合并的结果。下面的列表展示了结果:

administrator=administrator@example.com
sales=sales@example.com
support=support@example.co.uk

The child Properties collection’s value set inherits all property elements from the parent , and the child’s value for the support value overrides the value in the parent collection.

子Properties 集合的值集(value set)继承了父级的所有属性元素,并且support 值的子级值覆盖父级集合中的值。

This merging behavior applies similarly to the , and  collection types. In the specific case of the  element, the semantics associated with the List collection type (that is, the notion of an ordered collection of values) is maintained. The parent’s values precede all of the child list’s values. In the case of the Map, Set, and Properties collection types, no ordering exists. Hence, no ordering semantics are in effect for the collection types that underlie the associated Map, Set, and Properties implementation types that the container uses internally.

此合并行为同样使用于集合类型。在元素的特定情况下,与List 集合类型(即值的有序集合的概念)相关联的语义将得到维护。父列表的值位于子列表的所有值之前。对于Map、Set和Properties 集合类型,不存在排序。因此,对于容器内部使用的、以关联的 Map, Set和Properties 实现类型为基础的集合类型,没有有效的排序语义。

Limitations of Collection Merging(集合合并的限制)

You cannot merge different collection types (such as a Map and a List). If you do attempt to do so, an appropriate Exception is thrown. The merge attribute must be specified on the lower, inherited, child definition. Specifying the merge attribute on a parent collection definition is redundant and does not result in the desired merging.

不能合并不同的集合类型(例如 Map和List)。如果确实尝试这样做,则会抛出相应的异常。必须在较低的、继承的、子定义上(on the lower, inherited, child definition)指定merge 属性。在父集合定义上指定“merge ”属性是多余的,不会导致所需的合并。

Strongly-typed collection(强类型集合)

With the introduction of generic types in Java 5, you can use strongly typed collections. That is, it is possible to declare a Collection type such that it can only contain (for example) String elements. If you use Spring to dependency-inject a strongly-typed Collection into a bean, you can take advantage of Spring’s type-conversion support such that the elements of your strongly-typed Collection instances are converted to the appropriate type prior to being added to the Collection. The following Java class and bean definition show how to do so:

随着Java 5中泛型类型的引入,您可以使用强类型集合(strongly typed collection)。也就是说,可以声明Collection 类型,使其只能包含(例如)String 元素。如果使用Spring将强类型Collection 注入bean中,则可以利用Spring的类型转换支持(type-conversion support),以便在将强类型Collection 实例的元素添加到Collection 之前将其转换为适当的类型。以下Java类和bean定义说明了如何执行此操作:

Java

public class SomeClass {

    private Map accounts;

    public void setAccounts(Map accounts) {
        this.accounts = accounts;
    }
}

Kotlin

class SomeClass {
    lateinit var accounts: Map
}

    
        
            
                
                
                
            
        
    

When the accounts property of the something bean is prepared for injection, the generics information about the element type of the strongly-typed Map is available by reflection. Thus, Spring’s type conversion infrastructure recognizes the various value elements as being of type Float, and the string values (9.99, 2.75, and 3.99) are converted into an actual Float type.

当something bean的accounts属性准备好注入时,关于强类型Map 的元素类型的泛型信息(generics information)可以通过反射(reflection)获得。因此,Spring的类型转换基础设施将各种值元素识别为Float类型,字符串值(9.99、2.75和3.99)被转换为实际的Float类型。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/272383.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号