{ by david linsin }

April 17, 2007

Spring EJB integration

Last week I had to integrate Spring 2.0.x with EJB 2.x on JBoss 4.0.5. The problem I ran into was merging multiple ApplicationContext instances.

The actual integration was pretty straightforward, cause the Spring documentation is very good at that point. The real problems were cause by the packaging of our application, which yielded ClassCastExceptions and multiple distinct Spring contexts.

Our application is packaged as follows:

+ EAR
-+ WAR
-+ JAR1
-+ JAR2

The WAR defines a ContextLoadListener in it's web.xml and there's a applicationContext.xml file in the WEB-INF directory. JAR2 contains EJB components, which are using BeanFactoryLocator to initialize and use an ApplicationContext.

So we have 2 contexts, one initialized by the WAR and the second by JAR2. What we needed was a way to merge or link the two contexts. The Spring documentation is quite thin when it comes to this matter, at least I couldn't find any useful information regarding the problem. A blog post on CodeChimp gave the final hint. There is a way to define a hierarchic ApplicationContext, using context-param settings in web.xml. You simply define the ApplicationContext which is the parent and all other child contexts can reference to beans from that ApplicationContext. Another good article, which points out the problem is Migrating to Spring over at ONJava.com.

Another problem we ran into was a ClassLoader problem in Jboss. I think this is not JBoss specific, but I'm not really sure how other containers behave so I'm just gonna tell you what problems we had using JBoss 4.0.5. Since classes loaded in WAR and JAR2 have different ClassLoaders, instances are not treated as the same. So if we referenced a bean loaded through JAR2 in WAR, we would get a ClassCastException. We solved the problem by leveraging JBoss's specific class loading architecture. JBoss provides a ClassLoader, called JBossWebLoader, which disables class loading isolation between WAR and JAR2. This way we can share instances across archives, but we do give up isolation.

0 comments:


com_channels

  • mail(dlinsin@gmail.com)
  • jabber(dlinsin@gmail.com)
  • skype(dlinsin)

recent_postings

loading...