/  5
 
1
Spring ME
A framework so small, it makes you feel like a giantDecember 1, 2008
Table of Contents
1. Introduction ................................................................................................................. 12. How does it work? ........................................................................................................ 13. Show me an example .................................................................................................... 24. What else is supported? ................................................................................................ 45. What does the future behold? ........................................................................................ 56. Summary and Conclusions ............................................................................................. 5
1Introduction
During the last couple of years, Spring has essentially taken over the Enterprise world, by introducinga simplified and coherent programming model, and a powerful dependency injection framework.Unfortunately, if you were working on Java ME
, you were basically out of luck, for various reasons.First of all, 'classic' Spring is totally dependent on reflection, a capability not present in Java ME. Buteven if it
would 
be present, then the sheer size of the minimal Spring runtime would grow the size of your application beyond any reasonable numbers.That basically leaves the Java ME developers in the dark. In fact, coming from the EE world, it feelsawkward to start programming on Java ME. Once you got used to dependency injection, it's hard todo without.However, there islight at the end of the tunnel. This article introduces Spring ME. A framework that
does
work on Java ME, and moreover has no runtime dependencies
at all 
.
2How does it work?
Now, if 'classic' Spring relies on runtime reflection for dependency injection, Spring ME clearlyrequires something else. That 'something else' is build time analysis and code generation.The BeanFactories in 'classic' Spring are library-provided classes that use reflection for two purposes.First of all, it is used to gather data about the object graph to instantiate. Secondly, it is used toperform the construction and configuration of these objects.The BeanFactory in Spring ME is quite different. Instead of a class provided by the Spring runtime,it is a class that is generated, addressing the needs of your application explicitly. Instead of using
 
Spring ME2reflection to gather data about the object graph to construct, it relies on 'compile time' source codeanalysis only. And in order to instantiate the classes, it uses plain old vanilla Java code.In order to illustrate the difference, Example 1, “'Classic' Spring Approach” shows the classicapproach to Spring bean construction and configuration. It's a simplified example, and it omits theslew of exceptions that you would need to catch, but you probably get the picture.
Example 1. 'Classic' Spring Approach
Class cl = Class.forName("Person");Object instance = cl.newInstance();Method meth = cl.getDeclaredMethod("setName");method.invoke(instance, new Object[] { "Wilfred Springer" }); 
Example 2, “Spring ME Approach” illustrates the Spring ME approach. It looks ridiculously simple, andin fact, it is.
Example 2. Spring ME Approach
Person instance = new Person();instance.setName("Wilfred Springer"); 
The important thing to note here is that both examples could be based on the same snippet of Spring configuration, such as the snippet in Example 3, “Example configuration”. The 'classic' Springexample interprets it at runtime; Spring ME generates source from it at build time.
Example 3. Example configuration
<bean id="..." class="Person"><property name="name" value="Wilfred Springer"/></bean> 
Now, the smart readers might start wondering where the type information is coming from. After all,the bean configuration does not provide it. How do we know if the value "Wilfred Springer" actuallyrepresents a String, and not something else? A primitive int, for instance?
1
The answer is simple, thatdata is gathered build time as well, by analyzing the sources.
3Show me an example
In order to understand what this means in reality, let's look at a simple example example first. In thissimple example, we define two different classes: Teachers and Courses. A teacher can teach manycourses. Each of these courses has a certain topic.
1
Note that the code that would have to be generated in Example 2, “Spring ME Approach” would need to be different if the name propertywould be an int. I leave it as an exercise to the reader to figure out how it would be different.
 
Spring ME3
Example 4. Teacher and Courses
class Teacher {public void setName(String name);public String getName();public void setAge(int age);public void getAge();public void setCourses(List<Course> courses);public List<Course> getCourses();}class Course { public void setTopic(String topic);public String getTopic();}
Now, assume that we are going to create some instances of these classes by defining a teacher (RudyRucker), teaching a single course, called "The Fourth Dimension and Howe to Get There". Example 5,“Spring configuration” illustrates how you could do it.
Example 5. Spring configuration
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="..."><bean id="someTeacher" class="Teacher"><property name="name" value="Rudy Rucker"/><propertyname="courses"><list><bean class="Course"><property name="topic"value="The Fourth Dimension and How to Get There"/></bean></list></property></bean></beans> 
Everything you have seen so far is still plain vanilla Spring. Nothing fancy. Same syntax, same POJOapproach. But the next step is a Spring ME only step. In order to have BeanFactory capable of dealingwith this configuration, we need to
generate
that BeanFactory.You generate the BeanFactory by calling a Maven plugin. This is what you type on the commandline.
mvn spring-me:generate

Share & Embed

More from this user

Add a Comment

Characters: ...

springerwleft a comment

Spring ME 1.0 has been released recently. Simply reference it in your pom.xml file, and it will be downloaded automatically from the central Maven repository.