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"));
context.register(AppSpringConfig.class);
context.refresh();

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: https://github.com/fmarot/xml-mapping-tutorial/wiki

The Maven projects are available on Github too: https://github.com/fmarot/xml-mapping-tutorial

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

Soirée Grid Computing au JUG Paris & annonce JavaRebel

“Cache distribué”, “Network Attached Memory” et “Data Grid”, tel était le thème principal de la soirée du Java User Group de Paris. La présentation était tres intéressante et a fait bien réfléchir sur les meilleurs moyens de stocker les données selon les cas d’utilisation (peu d’écriture & beaucoup de lectures ou inversement etc…). Une telle présentation fait aussi prendre conscience des contraintes (et solutions !) qui se posent aux énormes sites d’e-commerce et de réservation en ligne (SNCF par exemple).
Pourtant, ce qui m’a le plus marqué, c’est la présentation du framework/librairie/utility JavaRebel. Je le connaissais déjà et avais déjà lu du bien de cet outils: il permet de modifier son code Java et de le recharger dans son application à la volée, de façon transparente, pour pouvoir tester immédiatement les modifs. Les développeurs Web me diront qu’ils peuvent déjà le faire avec les JSP… Mais là, on peut quasiment tout modifier (changer la signature des méthodes, ajouter des méthodes, faire des refactor du code) meme dans le code des controllers, des DTO, des objets métier etc… Et ceci sans être obligé de redéployer son appli pour pouvoir tester. La démo live Eclipse/Spring Pet Clinic qui nous a été faite était bluffante. Que de temps gagné pour les développeurs !
Mais la grosse news était quand meme l’annonce d’une version live qui pourrait (à terme car elle est actuellement en dev) agir sur LA VERSION DU CODE EN PRODUCTION et non plus simplement sur le poste du dévelopeur ! Cet outil pourrait analyser tout seul les modifications de code et dire si oui ou non les modifs sont applicables de façon transparente sans avoir à redéployer toute l’appli. Je trouve ca énorme, quelle avancée. A surveiller de prés donc.

Partage, mécénat global et… auto-satisfaction !

Aller, un peu d’auto satisfaction ne fait pas trop de mal, surtout lorsqu’elle fait avancer la cause du partage, alors je me lance: 2 idées que j’avais eu il y a quelques temps viennent d’etre reprises, et pas par n’importe qui: Mr Richard Stallman lui-même, et numerama, le site qui fait réfléchir sur les libertés numérique  ! Aller, je m’explique.

  1. récemment a été lancée l’idée du “Mécénat Global” par plusieurs personnes (y compris Mr Richard Stallman). Le site suivant explique leur idée. En fait, il s’agit plus ou moins d’une idée que j’avais eu il y a quelques temps (lors du débat sur la loi DADVSI) et dont je parlais dans ce post (2 ans déjà !!!). Basiquement, il s’agit d’un principe de license globale mais qui laisserait aux personnes le choix des artistes devant recevoir rémunération. Le nom de “Mécénat global” me semble particulierement bien choisi et claque bien. C’est marrant de voir un nom si parlant mis sur un concept auquel j’ai pensé 2 ans plus tot, mais sans le formaliser plus que ca !
  2. En lisant récemment cet article sur Numerama, j’ai sursauté en me revoyant moi-meme écrire ce post. La comparaison entre la duplication d’oeuvre numérique et de bien physiques y est semblable: elle est basée sur le pain du boulanger ! Enfin je vous laisse lire les 2 articles pour vous faire un idée.

En fait tout ca pour dire que d’une part, je suis content et un peu fier d’avoir des idées qui quelquefois ne sont pas mauvaises. Comme quoi ca m’arrive ;). par contre, je ne veut surtout pas sous-entendre que mes idées et articles de blog sont à la base des articles de Numérama et de l’idée de Mr Stallman. Je n’ai pas cette prétention ! Je suis bien conscient que de nombreuses personnes ont dans le monde, en des instants proches, des idées trés similaires. Néanmoins, peut etre que mes articles n’ont pas été lu, peut etre qu’ils l’ont été. Et dans ce dernier cas, ca renforce mes convictions: c’est le partage des idées et des données qui fait avancer la société. Pas le cloisonnement.

Protestation contre Hadopi: blackout

Le site passe en noir pour suivre le “blackout”initié par l’association “la quadrature du net“. Ceci, pour protester contre la prochaine loi Hadopi qui risque de brider internet, de faire condamner des innocent et qui est un ramassis de n’importe quoi: on entend meme parler de listes blanches pour les sites accessibles depuis les bornes wifi publiques. Internet limité à quelques milliers de sites. Wahou, on est de retour à l’époque du minitel: it feels so like it’s 1985 again :). Plus d’info sur ce mouvement ici.

Pour le minitel, je n’ai rien contre ! J’ai meme toujours trouvé que la France avait eu à l’époque une avance étonnante sur tous les autres pays.