Skip to content

Commit fc9d176

Browse files
committed
Revised c-namespace example
Issue: SPR-17601
1 parent aab4211 commit fc9d176

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/docs/asciidoc/core/core-beans.adoc

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -927,14 +927,14 @@ element.
927927
[subs="verbatim,quotes"]
928928
----
929929
<beans>
930-
<bean id="thingOne" class="x.y.ThingOne">
931-
<constructor-arg ref="thingTwo"/>
932-
<constructor-arg ref="thingThree"/>
930+
<bean id="beanOne" class="x.y.ThingOne">
931+
<constructor-arg ref="beanTwo"/>
932+
<constructor-arg ref="beanThree"/>
933933
</bean>
934934
935-
<bean id="thingTwo" class="x.y.ThingTwo"/>
935+
<bean id="beanTwo" class="x.y.ThingTwo"/>
936936
937-
<bean id="thingThree" class="x.y.ThingThree"/>
937+
<bean id="beanThree" class="x.y.ThingThree"/>
938938
</beans>
939939
----
940940
====
@@ -1937,27 +1937,28 @@ The following example uses the `c:` namespace to do the same thing as the from
19371937
xsi:schemaLocation="http://www.springframework.org/schema/beans
19381938
http://www.springframework.org/schema/beans/spring-beans.xsd">
19391939
1940-
<bean id="thingOne" class="x.y.ThingTwo"/>
1941-
<bean id="thingTwo" class="x.y.ThingThree"/>
1940+
<bean id="beanTwo" class="x.y.ThingTwo"/>
1941+
<bean id="beanThree" class="x.y.ThingThree"/>
19421942
1943-
<!-- traditional declaration -->
1944-
<bean id="thingOne" class="x.y.ThingOne">
1945-
<constructor-arg ref="thingTwo"/>
1946-
<constructor-arg ref="thingThree"/>
1947-
<constructor-arg value="[email protected]"/>
1943+
<!-- traditional declaration with optional argument names -->
1944+
<bean id="beanOne" class="x.y.ThingOne">
1945+
<constructor-arg name="thingTwo" ref="beanTwo"/>
1946+
<constructor-arg name="thingThree" ref="beanThree"/>
1947+
<constructor-arg name="email" value="[email protected]"/>
19481948
</bean>
19491949
1950-
<!-- c-namespace declaration -->
1951-
<bean id="thingOne" class="x.y.ThingOne" c:thingTwo-ref="thingTwo" c:thingThree-ref="thingThree" c:email="[email protected]"/>
1950+
<!-- c-namespace declaration with argument names -->
1951+
<bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
1952+
c:thingThree-ref="beanThree" c:email="[email protected]"/>
19521953
19531954
</beans>
19541955
----
19551956
====
19561957

1957-
The `c:` namespace uses the same conventions as the `p:` one (a trailing `-ref` for bean
1958-
references) for setting the constructor arguments by their names. Similarly, it
1959-
needs to be declared even though it is not defined in an XSD schema (it exists
1960-
inside the Spring core).
1958+
The `c:` namespace uses the same conventions as the `p:` one (a trailing `-ref` for
1959+
bean references) for setting the constructor arguments by their names. Similarly,
1960+
it needs to be declared in the XML file even though it is not defined in an XSD schema
1961+
(it exists inside the Spring core).
19611962

19621963
For the rare cases where the constructor argument names are not available (usually if
19631964
the bytecode was compiled without debugging information), you can use fallback to the
@@ -1968,12 +1969,15 @@ argument indexes, as follows:
19681969
[subs="verbatim,quotes"]
19691970
----
19701971
<!-- c-namespace index declaration -->
1971-
<bean id="thingOne" class="x.y.ThingOne" c:_0-ref="thingTwo" c:_1-ref="thingThree"/>
1972+
<bean id="beanOne" class="x.y.ThingOne" c:_0-ref="beanTwo" c:_1-ref="beanThree"
1973+
19721974
----
19731975
====
19741976

19751977
NOTE: Due to the XML grammar, the index notation requires the presence of the leading `_`,
19761978
as XML attribute names cannot start with a number (even though some IDEs allow it).
1979+
A corresponding index notation is also available for `<constructor-arg>` elements but
1980+
not commonly used since the plain order of declaration is usually sufficient there.
19771981

19781982
In practice, the constructor resolution
19791983
<<beans-factory-ctor-arguments-resolution,mechanism>> is quite efficient in matching

0 commit comments

Comments
 (0)