The Source for Java Technology Collaboration
User: Password:
Register | Login help    

Search

Online Books:
java.net on MarkMail:


A Dynamic Language For Bean Factory

Posted by unoinpiu on November 30, 2006 at 12:47 PM PST
A Dynamic Language For BeanFactory

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();
var locationService = new foo.core.LocationService();
var currencyExchangeService = new foo.core.CurrencyExchangeService();
{  // curly brackets ensure an unreachable scope for 'set'.
   var set = new HashSet()
   set.add("EUR");
   set.add("GBP")
   set.add("USD")
   currencyExchangeService.add(set)
}
var serviceManager = new foo.core.ServiceManager();
{  // curly brackets ensure an unreachable scope for 'set'.
   var set = new HashSet()
   set.addtimerService );
   set.addlocationService )
   set.addcurrencyExchangeService )
   currencyExchangeService.add(set)
}
var encoder = new foo.core.Encoder();
encoder.setMode(
"UTF-8");
var server = new foo.core.Server()
server.setHost("localhost");
server.setPort(9999);
server.setEncoder(encoder);
server.setServiceManager(serviceManager);

// 'native' objects could be used to indicate the scope of the beans
__scriptFactory.setBeanScope(server,"SINGLETON")


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.

Related Topics >> J2SE      
Comments
Comments are listed in date ascending order (oldest first)