Wednesday, February 28, 2007

Seamless Spring

I saw this post on TSS this morning. I read it, mostly because I like Seam so I wanted to see what was new with it. I was particularly pleased to see that Seam 1.2 was supposed to have "out of the box integration with Spring." I had to find out about this, so I read the man page on it.

I have to say, wow. The integration is really nice. When I started to read it, I immediately started to think "how is this going to work, Seam is all about statefulness and Spring is rather stateless?" Then I noticed the syntax on injecting a Seam component into a Spring bean:

<seam:instance name="someComponent"/>

I knew that when Spring 2.0 went XSD that we'd see things like this, i.e. a complimentary system integrating by importing its own XSD into a Spring config file. Next I noticed the unqualified name of the element, "instance." That sounded very stateful to me! Indeed from the doc:

Seam was designed from the ground up to support a stateful component model with multiple contexts. Spring was not. Unlike Seam bijection, Spring injection does not occur at method invocation time. Instead, injection happens only when the Spring bean is instantiated. So the instance available when the bean is instantiated will be the same instance that the bean uses for the entire life of the bean. For example, if a Seam CONVERSATION-scoped component instance is directly injected into a singleton Spring bean, that singleton will hold a reference to the same instance long after the conversation is over! We call this problem scope impedance. Seam bijection ensures that scope impedance is maintained naturally as an invocation flows through the system. In Spring, we need to inject a proxy of the Seam component, and resolve the reference when the proxy is invoked.

The <seam:instance/> tag lets us automatically proxy the Seam component.

That's just beautiful. Using a proxy like that is very common in both Hibernate and Spring, so this solution to the scope impedance problem is exactly what you would expect.

The doc details even more cool features. I love the way that Spring beans are injected into Seam components using @In("#{someSpringBean}"). The combination of using the annotation and the expression language is really nice. I would assume that something like @In("#{someSpringBean.someProperty}") would also work. Of course there are definitely some drawbacks to using all this syntactical sugar, but you can't argue that it's not fun.

Of course another interesting angle on this to me is that Seam is part of JBoss, which in some ways competes with Spring. Spring is often viewed as a "lighter" weight alternative to full app servers like JBoss. Indeed, when I met some JBoss developers last year they were somewhat incredulous about why people liked Spring. They thought it didn't do anything new, it just made some things a little easier...

technorati tags:, ,

No comments: