A Dynamic Language For Bean Factory
A Dynamic Language For BeanFactory.
One of the things I like the most about the Spring-IOC-container is its non-intrusiveness. I can write my classes, test them and then, only when I come to assemble them, use Spring. For this final step, I use the XmlBeanFactory (the most commonly used implementation of the BeanFactory) and write an xml file like this:
<beans>
<bean id="timerService" class="foo.core.TimerService"/>
<bean id="locationService" class="foo.core.LocationService" />
<bean id="currencyExchangeService" class="foo.core.CurrencyExchangeService">
<property name="currencies">
<set>
<value>EUR</value>
<value>GBP</value>
<value>USD</value>
</set>
</property>
</bean>
<bean id="serviceManager" class="foo.core.ServiceManager">
<property name="services">
<set>
<ref bean="timerService" />
<ref bean="locationService" />
<ref bean="currencyExchangeService" />
</set>
</property>
</bean>
<bean id="encoder" class="foo.core.Encoder">
<property name="mode" value="UTF-8"/>
</bean>
<bean id="server" class="foo.core.Server">
<property name="host" value="localhost" />
<property name="port" value="9999" />
<property name="encoder" ref="encoder" />
<property name="serviceManager" ref="serviceManager" />
</bean>
</beans>
This file is generally easy to understand and mantain. Besides the Spring-IDE makes my job easier and there are several articles on best practices on how to write a Spring xml file.
Yet, I can't stop wondering about a new BeanFactory which uses one of the many scripting languages for java to make this job simpler (and to an extent more natural). Let me give you an example. If I were to use Rhino (popular javascript engine for java) my configuration file could look like this: var timerService = new foo.core.TimerService();"UTF-8"); var server = new foo.core.Server(); |
It is your call to think which approach reads better.
Of course, writing this RhinoScriptBeanFactory would present new challenges. For example, we should probably limit the scripting capabilities neither to have conditional statements nor loops but allow just that bit of functionality that encourages the Dependency Injection pattern and nothing more.
- Login or register to post comments
- Printer-friendly version
- unoinpiu's blog
- 830 reads





