Thursday, November 30, 2006

New Spring 2.0 namespace


A bit unusual way for configuring the Spring beans seems to be the one Rod Johnson reminds us in this entry of his blog.
Basically a new "p" namespace allows us to use XML attributes instead of nested properties to configure the beans.
What we usually do in this way:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="employee" class="package.Employee">
<property name="company" ref="Company" />
<property name="weeklyHours" value="35" />
</bean>

<bean id="company" class="package.Company" />

</beans>
We could do it like this:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="employee" class="package.Employee"
p:direccion-ref="company"
p:weeklyHours="35" />

<bean id="company" class="package.Company" />

</beans>
A small change that could make our XMLs more compact, although I don't know if more readable.

More examples here.

No comments: