| 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 ,
、
administrator@example.org support@example.org development@example.org 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 , ,
, ,
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
注意在child 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
子Properties 集合的值集(value set)继承了父级
This merging behavior applies similarly to the , , and
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
当something bean的accounts属性准备好注入时,关于强类型Map



