Add an existing bean/object to a Spring ApplicationContext using JavaConfig

Here’s a (very) little post to demonstrate how to add a pre constructed instance to Spring in order to make it known by the ApplicationContext. Then you can inject it into any classic Spring-managed bean.

When would it be usefull ? Imagine you have a legacy application not using Spring. A new feature of this app uses Spring and needs to call methods of an object created by the legacy part (and thus unmanaged by spring). If you use the old XML Spring config, there are numerous exemples on the net. If you want to use the shiny new Javaconfig way, you may have a hard time figuring out how despite you know it must be so simple. Yes it is and here’s how:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getBeanFactory().registerSingleton("myBeanName", new String("String handled as Singleton in Spring"));

This is the simplest way I found. Then up to you to autowire this “legacy” bean in you brand new Spring managed code !


Tutorial: CXF Webservice using Maven & an XML Catalog

I’ve just published on Github a tutorial about separating the webservice (with WSDL) Maven project from the project containing the XSD. The goal is to enhance all projects overall testability.

The tutorial is available in the project wiki on Github:

The Maven projects are available on Github too:

JavaFX: the “whaou !” factor

Yesterday’s evening was Paris JUG (Java User Group) meeting dedicated to RIA technologies. The first presentation by Francois le Droff (from Adobe) was about Flex and the second one by Simon Ritter (from Sun) was about JavaFX.

Flash / Flex for the frontend, Java / Spring for the backend

First of all, let me state that I have never ever been a huge fan of Flash. From the early days of the web, I’ve always found that “simpler is better” and when I encountered website with flash based animated menu I was always horrified: it was so low, so “in your face”, so not standard… So my impressions may not be objective. But nevertheless I know that some useful and pro applications are made possible thanks to flash.
So basically, Francois started by presenting Flash, Flex (Flex is ActionScript + MXML for GUI building compiled into a swf file) and Air (the VM). Then he became more specific about Flash/Flex and Spring integration on the server. He demonstrated how a flash application sent to a client could communicate with Java/Spring code in the server. This allows to keep existing backend in Java while providing clients with the great experience (well, that’s the marketing terms, not my own thoughts 😉 ) of a flash based app. The thing is that now, Flash and Spring can be even more tightly integrated than before, when the “Spring factory” was used.
The new framework seems to offer synchronizing facilities. When the client is offline, modifies locally some bunch of data, and then is back online, then some conflict detection may be used. Moreover, thanks to the experience of Adobe in this field and the existing tools, the integration of artists work is said to be easy.
But the important problem for me lies with all the plumbing involved. Watching the presentation reminded me of the EJB 2 days where simple things required a lot of code, of configuration and the developer had to maintain many files coherent. It was very error prone. And even more here where 2 different languages are involved ! In the end, I feel like it may be a good technology but that may require a lot of efforts to deal with both the Java code AND the Flash code.

JavaFX, impressive in 2D. But only a toy ?

Regarding JavaFX, I was just curious because I never really took the time to dive into this new Framework/langage. Simon Ritter started with the basics of what JavaFX is. Basically, Java FX is a declaration language in which you describe what you want and how to display it instead of building from scratch your app. So its declarative nature takes it more toward HTML than toward Swing. It is said to be simpler to integrate UIDesigner and graphists work (the one done in Photoshop and Illustrator) but I don’t really see how… Nevertheless, apparently you can export their work from Adobe CS3 and CS4 files directly to JavaFX. And a Designer Tool is said to be ready quite soon (the marketing department did not allow Simon to show it if I remember well. What a shame !). What is sure is that JavaFX really seems a simpler Java2D than a “Swing 2” framework.
– First of all, the notion of ‘scene graph’ is fundamental. The elements are all grouped together with a parent/son relationship
– out of the box, many effects are available (motion blur, lightning shadows), plus transitions (fade, scale, rotate…), animations (timelines, keyframes…). They all allow to customize easily the components of a scene.
– medias can be integrated. Video for example are rendered at worst using a software codec if no hardware is available. And the Mediaview is a part of the scenegraph and treated as all other nodes (effects can be applied and so on…)
All of this reminded me of a 3 month internship I did in the video game studio Cyanide. At the time I worked on the menus (main GUI) of a rugby management game using a custom scripting language. What a shame we did not have JavaFX at that time ! I really think it can allow to create easily very good-looking menus even in videogames.

But the thing is that you still have to CODE your app. And for this, JavaFX makes thing (supposedly) easier by removing (most of the) need for the listener pattern in GUI because it provides the concept of *binding*. You can define that a variable (‘a’) depends on another variable (‘b’). So each time ‘b’ is modified, the new value of ‘a’ is computed again. The binding is optionally lazy.
I was quite disturbed by the lack of object orientation of its syntax though… For example you can write:

  • var revDays = reverse days
  • delete 31 from days
  • insert 5 into days

The summit of the presentation was the demos: Simon did a HUGE performance there, using Netbeans running on Ubuntu. It started with a round clock where the hands of the watch were replaced by concentric circles. With curved text on each circles ! But it was only a matter correctly positioning each character of the String, no magic involved here… Then he demonstrated the demo of a book: each page had a specificity (some writing, a video, a translucent page…) and you could turn each page with the mouse resulting in a realtime animation extremely realistic. It was a bit like this demo, but much more advanced. The crowd really like this one and applauded loud. The last demo was the ‘magical’ one. It was inspired by Johnny Chung Lee’s work with the wiimote. The impressive part was when Simon handed a plastic frame equipped with IR-sensors in front of both a wiimote linked to the laptop (thanks to a Java bluetooth stack) and a videoprojector. A card was displayed by the computer+projector onto the frame, and when Simon moved (or flipped !) the frame, the program moved the image accordingly. In realtime ! Even if its hard to describe it here, believe me: that was amazing and the crowd gave an ovation. It reminded me of the Natal project that Microsoft recently showed at E3, on a lower scale though.
In the end, I think that JavaFX has the opportunity to become something very useful: it can create wonderful good-looking application while staying in the Java World. The advantage of using FX for the front end GUI is the same as using Groovy as a scripting language: it enhances the possibilities while leveraging all the existing code and staying on the well-known VM and Java ecosystem. But Simon did not talk at all about the communication between the fat FX client and the backend server. Whether it involves as much plumbing as Flex does or not, I don’t know. I just hope not because, after all, it’s all “Java” !

What’s the future ?

To conclude we have 2 great technologies. But I think, some questions remain:
It seems we can do more and more extraordinary stuffs in basic Javascript. Why use flash or Java FX ? And what’s their advantage versus Google Gears ?
Flash seems not really SEO compatible (although an agreement has been made with Google to reference flash based app content), and deep linking is not a very trivial task (although Deezer made it, each state of the app has a different URL). I think it’s the same for JavaFX.
Nevertheless, the future looks promising and I can’t wait to see JavaFX become mainstream !

minor edit on 2009-07-14: added links + corrected typos

Use ANT to auto-generate the list of jar files in a MANIFEST.MF

Writing a manifest by hand is both error prone & boring. Moreover, if you change the dependancies, add/remove libraries jars from you project, then you have to modify again the manifest file…
One method is to use ANT to create the manifest for you, and more specifically the “Class-Path” line in the manifest.
The following exemple speaks for itself and generate a manifest file as listed in my previous post about jar files (

<path id=”library.Project”>
<fileset dir=”../lib/”>
<include name=”**/*.jar”/>

<!– This is the important line: by specifying in the “jarfile” attribute the location of the
generated jar, ANT will be able to deduce the relative path to you jar libraries –>
<manifestclasspath property=”lib.list” jarfile=”.”>
<classpath refid=”library.Project” />

<!– Compile –>
<target name=”compile” depends=”build.env” description=”Compiles tmmerge Java source code.”>
<mkdir dir=”${build.dir}”/>
<javac srcdir=”${src.dir}” destdir=”${build.dir}” deprecation=”true” debug=”true” optimize=”true”>
<path refid=”class.path” />

<!– JAR –>
<jar jarfile=”${SOFT_PACKAGE}/${}” basedir=”${build.dir}”>
<attribute name=”Class-Path” value=”${lib.list}”/>
<attribute name=”Main-Class” value=”fr.alcatel.ascc.tmmerge.TMMerge”/>
<attribute name=”Product-Name” value=”TMMerge”/>
<attribute name=”Package-Title” value=”fr.alcatel.ascc.tmmerge”/>
<attribute name=”Package-Version” value=”${software.version}”/>


How-to java jars and MANIFEST.MF

Here’s a brief “how-to” for remembering what is possible with a jar file, how to use it, launch its main class and define where the other jars (the libraries used) are stored. I could not find anywhere on the net a good explanation of all this, so decided to write my own.
So imagine you have your own jar file named myApp.jar which main class (the one with the “main” method) is marot.francois.MyMainClass which needs to receive 2 arguments: arg1 and arg2 (passed in String[] args).

1- Without using any MANIFEST.MF fil

If you don’t have a MANIFEST.MF file in your jar, or if you want to set some specific places where Java should look for the libraries used by myApp.jar, then you should use the following command. Beware, the use of the star is only available since Java 6 (or is it Java 5 ?)

on Windows:
java -cp tmmerge.jar;myCustomLibPath1\*;myCustomLibPath2\*;myCustomLibPath3\* marot.francois.MyMainClass arg1 arg2

on Linux:
java -cp tmmerge.jar:myCustomLibPath1\*:myCustomLibPath2\*:myCustomLibPath3\* marot.francois.MyMainClass arg1 arg2

Same as previous one but with all the libraries’ jars listed instead of using *:
on Windows:
java -cp ./myApp.jar;myCustomLibPath1\sctm.jar;myCustomLibPath2\ant.jar;myCustomLibPath2\ant-launcher.jar;myCustomLibPath3\scdata.jar marot.francois.MyMainClass arg1 arg2

on Linux:
java -cp ./myApp.jar:myCustomLibPath1\sctm.jar:myCustomLibPath2\ant.jar:myCustomLibPath2\ant-launcher.jar:myCustomLibPath3\scdata.jar marot.francois.MyMainClass arg1 arg2

At first, I thought such a command line was overriding the classpath in MANIFEST.MF, if any. But that is not the case, the MANIFEST.MF is just bypassed.

2- Using a MANIFEST.MF file in the jar

Launch myApp using the jar files listed in myApp.jar’s own manifest
java -jar myApp.jar arg1 arg2

The content of the META-INF\MANIFEST.MF file must be:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: 1.6.0_02-b06 (Sun Microsystems Inc.)
Main-Class: marot.francois.MyMainClass
Product-Name:  myApp
Package-Title: marot.francois
Package-Version: 1.00.00
Package-Vendor: Elsys Design Avisto
Class-Path: myCustomLibPath2/ant-launcher.jar myCustomLibPath2/ant.jar myCustomLibPath3/scdata.jar myCustomLibPath1/sctm.jar

In a next post, I’ll show you how to use ANT to automatically generate the content listed here. And more specifically the list of libraries which would be error prone to write by hand.
By the way, I think it’s good to point to something important: in the “Class-Path”line of the manifest, you CAN’T use a star as you could do when specifying the classpath on the command line.
Also be aware that the space is the jars path delimiters as opposed to the “;” (windows) or “:” (Linux) on the command line. This last remark is very very error prone…

3- What I would like to be able to do (but it seems like Java doesn’t handle this case)

Warning: the exemple given hereafter does not work. I spent a lot of time trying to find some explanation of the reasons why, but was not able to find.
So the problem seems to be that you can’t override the default classpath specified in the jar’s manifest if you use the -cp switch at the same time. So the basic rule of thumb is:
“in java, do not use both switches -jar and -cp at the same time”: it does not work (at least up to Java 6).
I thought it would be cool to be able to run a jar but defining another place where it can find its dependancies. In case you want the users of your app to be able to use a shared folder containing the lib jars but don’t want him to have to know the main class’ name.
Here’s the command line I desperatly tryed to run:
REM java -cp myCustomLibPath1\*;myCustomLibPath2\*;myCustomLibPath3\* -jar myApp.jar arg1 arg2

Nevertheless, the same goal can be reached (successfuly) by using method 1. Only that the user needs to know the main class.

GroowinLauncher: win utility for your USB key

I’ve just released a small utility I’ve made. It ‘s named GroowinLauncher because it is made in Groovy and it creates a small icon in your taskbar to allow you to launch easily your favourite apps. Recently, I switched a lot from computers to computers having to modify a lot of files. I was tired to always have to bring my favourite softwares with me (namely Winmerge, Notepad++ and 7zip) to do my job. Moreover, my USB key is very slowwwwwwww & it was a pain to copy the softs. And, last but not least, I wanted to always have a Java & Groovy dev environment in order to be able to quickly develop small utility or batch process some files.

So I created my own launcher which:

  1. contains my favourite softwares & Java / Groovy environment
  2. is designed to be installed from slowww USB devices
  3. sits in the taskbar to allow to launch the softwares easily
  4. does not pollute the computer (it installs itself in Windows %TEMP% dir and that’s all)
  5. you can add your own app by editing a simple XML file and 7zipping it

Here’s how it looks:

GroowinLauncher a groovy utility

So download this file: and unzip it on your USB key. Then, when you want to install it on the %TEMP% dir of the computer, just double click on InstallToTMP.bat. It will automatically copy, unzip & run the required files, then launch the launcher (!).

If you ever need a Java or Groovy dev environment, just:

  1. run a command shell (start menu -> run -> cmd )
  2. type “cd /D %TEMP%\GroowinLauncher\”
  3. execute the setenv.bat command
  4. that’s it, you can run & use Groovy !

(tested on Windows XP SP2, the Groovy version provided is 1.5.4 & Java is Java6u3)