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
<list/>
,
<set/>
,
<map/>
, and
<props/>
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:
<list/>、<set/>、<map/>和<props/>元素分别設定Java集合類型
List
,
Set
,
Map
和
Properties
的屬性和參數。以下示例展示了如何使用它們:
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
<props>
<prop key="administrator">[email protected]</prop>
<prop key="support">[email protected]</prop>
<prop key="development">[email protected]</prop>
</props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="an entry" value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>
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
<list/>
,
<map/>
,
<set/>
or
<props/>
element and have child
<list/>
,
<map/>
,
<set/>
or
<props/>
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)。應用程式開發人員可以定義父級<list/>、<map/>、<set/>或<props/>元素,并讓子級<list/>、<map/>、<set/>或<props/>元素繼承和重寫父集合中的值。也就是說,子集合的值是合并父集合和子集合的元素的結果,子集合的元素覆寫父集合中指定的值。
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):
<beans>
<bean id="parent" abstract="true" class="example.ComplexObject">
<property name="adminEmails">
<props>
<prop key="administrator">[email protected]</prop>
<prop key="support">[email protected]</prop>
</props>
</property>
</bean>
<bean id="child" parent="parent">
<property name="adminEmails">
<!-- the merge is specified on the child collection definition -->
<props merge="true">
<prop key="sales">[email protected]</prop>
<prop key="support">[email protected]</prop>
</props>
</property>
</bean>
<beans>
Notice the use of the
merge=true
attribute on the
<props/>
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
屬性的<props/>元素上使用了merge=true屬性。當容器解析并執行個體化
child
bean時,生成的執行個體具有一個
adminEmails
屬性集合,該集合包含将
child
bean的adminEmails集合與parent bean的adminEmails集合合并的結果。下面的清單展示了結果:
[email protected]
[email protected]
[email protected]
The child
Properties
collection’s value set inherits all property elements from the parent
<props/>
, and the child’s value for the
support
value overrides the value in the parent collection.
子
Properties
集合的值集(value set)繼承了父級<props/>的所有屬性元素,并且
support
值的子級值覆寫父級集合中的值。
This merging behavior applies similarly to the
<list/>
,
<map/>
, and
<set/>
collection types. In the specific case of the
<list/>
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/>集合類型。在<list/>元素的特定情況下,與
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<String, Float> accounts;
public void setAccounts(Map<String, Float> accounts) {
this.accounts = accounts;
}
}
Kotlin
class SomeClass {
lateinit var accounts: Map<String, Float>
}
<beans>
<bean id="something" class="x.y.SomeClass">
<property name="accounts">
<map>
<entry key="one" value="9.99"/>
<entry key="two" value="2.75"/>
<entry key="six" value="3.99"/>
</map>
</property>
</bean>
</beans>
When the
accounts
property of the
something
bean is prepared for injection, the generics information about the element type of the strongly-typed
Map<String, Float>
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<String, Float>
的元素類型的泛型資訊(generics information)可以通過反射(reflection)獲得。是以,Spring的類型轉換基礎設施将各種值元素識别為Float類型,字元串值(9.99、2.75和3.99)被轉換為實際的Float類型。