Skip to main content

DIME - Dependency Injection for Java ME

Posted by brunogh on April 27, 2010 at 3:55 PM PDT

My masters colleague, Guilherme Hobold, and his friend, Elinton Machado, have created a very interesting project for helping Java ME applications with dependency injection. Actually, it is not only for Java ME (CLDC), but because of there is no reflection, they have done some magic to make it work in a nice way.

The project is called DIME, which means Dependency Injection for Java ME. Basically it has a XML to configure the beans (it depends on kXML lib for parsing), a container to get the beans and the classes have to implemented a Bean interface that provides access to the hash table that is used to set the correct values. The XML file uses the same notation as Spring.

Here is some sample code that I got from the project page.

XML file (also supports map and other types).
<?xml version="1.0"?>
<beans>
      <import resource="other_beans.xml" />

      <bean id="fooSet" class="test.collection.FooList">
            <property name="elements">
                  <set>
                        <value>element1</value>
                        <value>element2</value>
                  </set>
            </property>
      </bean>

      <bean id="Foo" class="test.Foo">
            <property name="filename">
                  <value>movies2.txt</value>
            </property>
            <property name="fooSet" ref="fooSet"/>
      </bean>
</beans>

Make a bean:
import java.util.Hashtable;
import org.dime.Bean;
import test.collection.FooList;

public class Foo extends FooAbstract implements Bean {

      private String filename;
      private FooList fooSet;

      public String getFilename() {
            return filename;
      }

      public void setProperties(Hashtable properties) {
            filename = (String)properties.get("filename");
            fooSet = (FooList)properties.get("fooSet");
      }
}

Load the XML file and get the bean:
BeanContainer container = new ClassPathXmlBeanContainer("file.xml");
Foo foo = (Foo)container.getBean("Foo");
System.out.println(foo.getFilename());

Very nice job! Take a look in the project page for more information. Take a try!

http://dime.dev.java.net

Cheers,
Bruno Ghisi

Related Topics >>

Comments

Spring ME

I could be wrong, but it seems the objectives are comparable to Spring ME. Check out the JavaOne slides here: http://www.slideshare.net/springerw/spring-me-javaone. Spring ME itself is here: http://springframework.me/

DIME Vs Spring ME

Hi, You are right about Spring ME. But DIME is really different about Spring ME. The objectives are the same, both provide Dependency Injection to the J2ME platform. When using Spring ME you must have a Maven plugin to generate the BeanFactory in compilation time. Even when using DIME, the only thing you have to do is configure your XML and pass it to the BeanContainer. So the BeanContainer will read the XML file in execution time. The BeanFactory from Spring ME is genereted like this:

public class BeanFactory {

public Object getBean(String name){
if ("movieFinder".equals(name)){
return getMovieFinder();
}
}

public final Object getMovieFinder(){ .. }

}

There will be one if condition inside getBean method for each bean you have in your XML file. So the XML file is loaded and the BeanFactory class is generated before you generate your distributed package.
Beyond that DIME offers more beans attributes in XML file like scope and lazy. The first one allow you to create beans that are singletons or beans that are created every time you call getBean(). The lazy attribute tells the container if the bean must be created as soon as XML file is loaded.

To finish, I think DIME is more flexible and easy to use when comparable with Spring ME.

That`s it.

Cheers,
Guilherme Hobold.