From 1feb389e2756c05cc40e6c2c2831823b558bbfd9 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Wed, 15 Dec 2010 16:12:54 +0000 Subject: [PATCH] SPR-7470 + add basic documentation for c: namespace git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@3814 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../src/beans-dependencies.xml | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/spring-framework-reference/src/beans-dependencies.xml b/spring-framework-reference/src/beans-dependencies.xml index bbb9fc04da0..282731cf4e4 100644 --- a/spring-framework-reference/src/beans-dependencies.xml +++ b/spring-framework-reference/src/beans-dependencies.xml @@ -1029,6 +1029,53 @@ support=support@example.co.uk time. + +
+ XML shortcut with the c-namespace + + Similar to the , the c-namespace, newly introduced in Spring 3.1, + allows usage of inlined attributes for configuring the constructor arguments rather then nested constructor-arg + elements. + + Let's review the examples from with the c namespace: + + <beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:p="http://www.springframework.org/schema/c" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans.xsd"> + + <bean id="bar" class="x.y.Bar"/> + <bean id="baz" class="x.y.Baz"/> + + <-- 'traditional' declaration --> + <bean id="foo" class="x.y.Foo"> + <constructor-arg ref="bar"/> + <constructor-arg ref="baz"/> + <constructor-arg value="foo@bar.com"/> + </bean> + + <-- 'c-namespace' declaration --> + <bean id="foo" class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="foo@bar.com"> + +</beans> + + The c namespace uses the same conventions as the p one (trailing -ref for bean references) + for setting the constructor arguments by their names. And just as well, it needs to be declared even though it is not defined in an XSD schema + (but it exists inside the Spring core). + + For the rare cases where the constructor argument names are not available (usually if the bytecode was compiled without debugging information), one can + use fallback to the argument indexes: + + <-- 'c-namespace' index declaration --> +<bean id="foo" class="x.y.Foo" c:_0-ref="bar" c:_1-ref="baz"> + + Due to the XML grammar, the index notation requires the presence of the leading _ as XML attribute names cannot start + with a number (even though some IDE allow it). + + In practice, the constructor resolution mechanism is quite efficient in matching arguments so + unless one really needs to, we recommend using the name notation through-out your configuration. +
Compound property names