|
|||||
|
|||||
Syntax Compared
This comparison is made between Spring and Nuts. I'm not familiar with the script support by NanoContainer, so I'll skip the comparison with Pico. Concise Collection LiteralIn Spring, the syntax to create a Properties object is like: <properties> <property name="name"><value>Tom</value></property> <property name="age"><value>10</value></property> </properties> And syntax for list/array is: <list> <value>Tom</value> <value>10</value> </list> Using Nuts' Concise Collection Literal, they can be written as: <value val="{name=Tom, age=10}"/>
and <value val="Tom, 10"/>
Quite some pointed brackets disappeared. Such concise collection literal can be used to specify property values and parameter values for components. For example: <bean id="account" class="BankAccount" props="{name=Tom,age=10}"/> Another example: <ctor id="account" class="BankAccount" args="Tom,10"/> Now Compare with Spring: <bean id="account" class="BankAccount"> <properties> <property name="name"><value>Tom</value></property> <property name="age"><value>10</value></property> </properties> </bean> Another Spring: <bean id="account" class="BankAccount"> <constructor-arg index="0"><value>Tom</value></constructor-arg> <constructor-arg index="1"><value>10</value></constructor-arg> </bean> This is just one or two beans. Imagine a configuration file with 100 components/beans. Auto-RegistrationTalking about 100 components, it is sometimes possible that these 100 components only differ in the class name but share the same way of wiring (say, auto wire by type). Nuts provides a feature called "dynamic registration" that can help registering all these components in one shot. Instead of writing: <bean id="Customer" class="com.mycompany.Customer" autowire="bytype"/> <bean id="Person" class="com.mycompany.Person" autowire="bytype"/> <bean id="Bank" class="com.mycompany.Bank" autowire="bytype"/> <bean id="Account" class="com.mycompany.Account" autowire="bytype"/> <bean id="Address" class="com.mycompany.Address" autowire="bytype"/> ... <bean .../> <bean .../> <bean .../> <bean .../> <bean .../> <bean .../> we can say: <nut name="define" class="jfun.yan.xml.nuts.optional.ForeachRegisterNut"/> <body> <define names="Customer,Person,Bank,Account,Address"> <function params="name"> <bean class="com.mycompany.${name}" property-names="*" autowire="bytype"/> </function> </define> </body> Or, if the class names and keys are saved in a properties file, we could say: <nut name="register" class="jfun.yan.xml.nuts.optional.PropertiesRegisterNut"/> <body> <register resource="/com/mycompany/domains.properties"> <function params="id,name"> <bean class="$name" property-names="*" autowire="bytype"/> </function> </register> </body> (You can see Dynamic Registration Tags for detailed explanations.) As a result, only one intead of a thousand top-level tags is needed. A lot of tedious configuration work is saved. Custom tagSpring allows implementation of custom logic via FactoryBean. It works, but the syntax isn't very pleasant: <bean class="com.mycompany.SomeLogicFactoryBean"> <property key="attr1">value1</property> <property key="attr2">value2</property> </bean> Nuts allows custom tags to be built so that custom logic can be expressed in a more natural syntax: <somelogic attr1="value1" attr2="value2"/> SummaryThe difference is obvious. Who said XML configuration is always a "Yuk!"? Created by benyu |
|||||
|
Copyright 2003-2006 - The Codehaus. All rights reserved unless otherwise noted.
Powered by Atlassian Confluence
|
|||||