Spring Bean and Factory Bean can be managed by Nuts.
Syntax
Here's an example, for a regular Spring bean configuration:
<bean id="personTarget" class="com.mycompany.PersonImpl"> <property name="name"><value>Tony</value></property> <property name="age"><value>51</value></property> </bean>
The corresponding Nuts configuration is very similar:
<bean id="personTarget" class="com.mycompany.PersonImpl"> <prop key="name" val="Tony"/> <prop key="age" val="51"> </bean>
Spring FactoryBean and AOP can also be represented in Nuts. For the following Spring interceptor:
<bean name="myRawController" class="com.javalobby.tnt.spring.aop.ExampleController" /> <!-- Create the proxy bean that returns AOP'd varieties of our controller --> <bean name="myController" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="interceptorNames"> <list> <value>pointcut.advisor2</value> <value>pointcut.advisor1</value> <value>myRawController</value> </list> </property> </bean>
A corresponding Nuts syntax is:
<bean id="myRawController" class="com.javalobby.tnt.spring.aop.ExampleController" /> <!-- Create the proxy bean that returns AOP'd varieties of our controller --> <bean id="myController" class="org.springframework.aop.framework.ProxyFactoryBean"> <prop key="interceptorNames" val="pointcut.advisor2,pointcut.advisor1,myRawController" /> </bean>
Spring Lifecycle
Spring life cycle can be directly ported to Nuts. Both Nuts style "initializer", "disposer" atributes and Spring style "init-method", "destroy-method" can be used on <ctor>, <bean>, <method> tags.
For example:
<bean id="mybean" class="BankAccount" init-method="init" destroy-method="destroy"/>
or
<bean id="mybean" class="BankAccount" initializer="init" disposer="destroy"/>
Spring Plugin
The core of Yan and Nuts has no dependency on Spring. The reason Spring's beans and FactoryBean can be used inside Nuts is because of Nuts' flexible plug-in tag system. A few spring-specific Nut classes are included in the spring integration package. <ctor>, <bean>, <method> tags are customized to support Spring beans.
In order to enable spring tags, the jfun.yan.xml.nuts.spring.SpringNuts class needs to be used. The following code creates a NutsProcessor object that's aware of Spring beans:
NutsProcessor processor = new NutsProcessor(); SpringNuts.setSpringAware("spring processor", processor);
This NutsProcessor object can then be used to process a configuration file that contains spring beans:
processor.processFile("test_spring.xml");
Declarativeness Plugin
Transactional conrol is made declarative in Nuts using the customized tags.
For the following spring transactional control that needs an explicit TransactionProxyFactoryBean:
<bean id="petStoreTarget" class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl"> <property name="accountDao"><ref bean="accountDao"/></property> <!-- Other dependencies omitted --> </bean> <bean id="petStore" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="transactionManager"/> <property name="target" ref="petStoreTarget"/> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED,readOnly</prop> </props> </property> </bean>
Nuts can simplify it to:
<bean id="petStoreTarget" class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl" transactionAttributes="$txprops"> <local> <props id="txprops"> <entry key="insert*" val="PROPAGATION_REQUIRED"/> <entry key="update*" val="PROPAGATION_REQUIRED"/> <entry key="*" val="PROPAGATION_REQUIRED,readOnly"/> </props> </local> <prop ke="accountDao" val="$accountDao"/> < <!-- Other dependencies omitted --> </bean>
The "transactionalAttributes" is made an attribute of the <bean> tag, thus more declarative. The missing "transaction manager" is autowired bytype. Of course, you can always explicitly specify it as:
<bean ... transactionManager="$txmanager">
<!-- omitted properties -->
</bean>
In fact, we can customize the tags to express any logic with attributes and sub-elements, as long as we feel it's important enough.
Created by benyu
On Sun Nov 20 21:33:56 CST 2005
Using TimTam
