Asked this on the springframework-user mailing list but got no replies, so I'm trying my luck here.
I'm trying to use Spring 2.5.1 in an EAR on both the EJB3 side and the Web app (WAR) side (built using Maven 2, deployed on Glassfish v2).
The entire project structure follows that of the Maven 2 simple J2EE archetype, with a root project that defines sub-projects "abc-ear" depends on "abc-web" depends on "abc-ejb".
On the EJB3 side, I'm using the SpringBeanAutowiringInterceptor to inject my dependencies into my (annotated) EJBs. I have a "beanRefContext.xml" that merely creates a ClassPathXmlApplicationContext which contains the actual bean wirings.
On the WAR side, I'm using Spring + Spring MVC, so the DispatcherServlet picks up the Spring configuration from "WEB-INF/abc-servlet.xml".
I have no problems when building and deploying each layer individually, whether from the command-line (Maven2) or my IDE (Eclipse 3.3). As an aside, I had to place the Spring framework JAR in the container's "lib" directory so the EJB JARs (which don't support packaging dependent JARs) can find and use it. This meant that my WAR (and my EAR) don't have a separate copy of the Spring JARs.
However, when the EAR project is built, and I try to deploy the EAR artifact, I run into an exception when my EJB is being instantiated. Here's a snippet of the exception trace:
I figure that basically Spring is finding two separate objects that implement the BeanFactory interface when both the EJB JAR and the WAR are deployed in a single EAR.Code:[#|2008-02-22T15:22:03.621+0800|INFO|sun-appserver9.1|javax.enterprise.system.container.ejb|_ThreadID=21;_ThreadName=httpSSLWorkerThread-8080-0;|EJB5070: Exception creating stateless session bean : [{0}] org.springframework.beans.factory.access.BootstrapException: Unable to return specified BeanFactory instance: factory key [null], from group with resource name [classpath*:beanRefContext.xml]; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.beans.factory.BeanFactory] is defined: expected single bean but found 2 at org.springframework.beans.factory.access.SingletonBeanFactoryLocator.useBeanFactory(SingletonBeanFactoryLocator.java:410) at org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.getBeanFactoryReference(SpringBeanAutowiringInterceptor.java:132) at org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.getBeanFactory(SpringBeanAutowiringInterceptor.java:113) at org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor.autowireBean(SpringBeanAutowiringInterceptor.java:93) ... Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.beans.factory.BeanFactory] is defined: expected single bean but found 2 at org.springframework.beans.factory.BeanFactoryUtils.beanOfType(BeanFactoryUtils.java:379) at org.springframework.beans.factory.access.SingletonBeanFactoryLocator.useBeanFactory(SingletonBeanFactoryLocator.java:400) ... 68 more |#]
All the above seems like a common scenario, so I'm wondering if anyone else has successfully done what I'm trying to accomplish? Basically, how do I configure Spring in both my EJB and WAR layer so they deploy nicely when in an EAR?


Reply With Quote
