With the growth of annotations XML configuration files continue to reduce in size (In some cases they have disappeared). I have never really been a fan of Annotations, especially those that bring in information that was best left outside Java code.
So whenever I come across an XML simplification I get pretty excited (Though I may never get to use it). I had earlier written about the p namespace and how its use reduces the file size. The p namespace was useful when we had getter based wiring. However if you have constructor based wiring than the p namespace is of no real use.
So if wanted to wire a Simple java like this one
So whenever I come across an XML simplification I get pretty excited (Though I may never get to use it). I had earlier written about the p namespace and how its use reduces the file size. The p namespace was useful when we had getter based wiring. However if you have constructor based wiring than the p namespace is of no real use.
So if wanted to wire a Simple java like this one
publicclass Simple {than the configuration would be :
publicString value;
public Simple(finalString value) {
this.value = value;
}
}
<bean id="rootBean" class="com.Simple">As seen here, for the constructor injection we use the <constructor-arg> element. This can be now simplified as :
<constructor-arg type="java.lang.String" value="Root"/>
</bean>
<beans xmlns="http://www.springframework.org/schema/beans"The c prefix is followed by the name of the constructor argument. Hence "c:value". This runs fine.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="simple" class="com.c_namespace.Simple"c:value="Hello World!"/>
</beans>
Aug 15, 2013 9:43:26 AM org.springframework.beans.factory.xml.What if we have a bean as the constructor argument ?Consider the class :
XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [c_ns.xml]
Hello World!
publicclass SimpleDetail {If we had to define this bean then the configuration would be :
public Simple simple;
public SimpleDetail(final Simple simple) {
this.simple = simple;
}
}
<bean id="simple" class="com.c_namespace.Simple" c:value="Hello World!"/>It is the <property name>-ref format when dealing with bean as a reference. Just like the p namespace, there is no XSD file defined for c- namespace. It is simply a shortcut method provided for use in the XML configuration
<bean id="simpleD" class="com.c_namespace.SimpleDetail"c:simple-ref="simple"/>