Nov 6th, 2006, 01:17 PM
Flex/Spring Best Practices
I am a previous J2EE developer who has been doing a lot of coding for Flex (1.5) using Tomcat as a web app container. In the past we have been using servlets with HTTP calls from Flex and passing XML back and forth. Some of my co-workers were using remote objects and we have recently moved to communicating back and forth using Flex’s remote object calls.
We are now looking at (read implementing a project) using Spring/Ibatis. Coming from absolute zero Spring experience I have been working on understanding it and, more importantly, how to use it properly. I have an implementation that is functioning but I have doubts about whether I am using “good practices”. One of my main struggles is that although there are examples of using Flex with Spring, these are simple cases that do not explain good “rules of thumb” of how to use Spring in the context of having Flex as the front end.
I have some specific questions whose answers will probably lead to further discussion.
1) If I have “business logic” in my application what is the best way for one piece of business logic call another piece of business logic in Spring?
In my application I have a service that manipulates files (FileService.java). One thing it does is read information from a file on the server and will later load the information into a database. I have another service, Mapping Service, that has methods that take information on how the data from the file will be read into the database. I want the mapping service to call methods on the file service to perform certain functions.
Each service is a Spring Bean that is defined in the application.xml file. The mapping service has a property that is a reference to the file service. In a method of the mapping service I simply call:
Since, Spring injects the mappingForm object this works.
Is this the “proper” way for one object in Spring to use another object in Spring?
2) What are some of the “rules of thumbs” or best practices when deciding if a parameter should be passed to a method in a Spring bean and when an injected object should be used instead
So I have passed in an object to my Spring context (right now I don’t care how). The parameter object came from a context that does not have access nor can it create a Spring context. That object is used in multiple spring beans. Should that object just continually be passed as a parameter or should a spring bean be set? What if the object is a String? What if the object is a more complex object with fields/getters/setters?
3) When passing information into a Spring context from outside the context, is it best to pass the information in using a generic construct (Map or List) and keep passing it around the Spring context in this construct or is it best to transform it into a Spring Bean that is used by application beans?
I have the ability to create a “mirror” object in my Flex application that looks just like any object in Java. When Flex calls the Spring object (on the server) then the Flex gateway knows how to create a Java object out of that Actionscript (Flex’s ECMAScript that is used on the client) object. That Java object with the information from the client is then passed into the Spring bean as a parameter. From here I can either pass that generic object around (see question above) or transform it into a Spring bean and only operate on it in the Spring context. I can then pass that object back to the client and Flex can using it as an ActionScript object. The advantages of this is that you can map UI components directly to object fields in Flex to reduce the coding required to load UI components and object values from changes by the user.
It has been brought to my attention that it might be a better practice to pass in something like a Hash Map into Spring. This would be very easy because Flex’s default behavior passes ActionScript objects to a Java server using Hashmaps; you have to add a line of code to your Flex app to couple it with a server-side object. But even if I pass it in as a map should I just pass the information around as a map or transform it into a Spring bean?
Well, I think that’s a good starting point. The key here is that Flex is a different type of front end from traditional interfaces used. It can be looked at as a remote server in a way. I just haven’t seen a lot of discussion on good practices to use with something like Flex (as opposed to Struts or Spring’s MVC).