Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Spring beans from other Maven modules inside a WAR?

I have a new web app that is packaged as a WAR as part of a multi-module Maven project. The applicationContext.xml for this WAR references beans that are imported from the "service" module, which in turn imports beans from the "dao" module. The import statement in applicationContext.xml looks like this:

<import resource="classpath*:service.xml" />

and the one inside the service.xml file looks like this:

<import resource="classpath*:dao.xml" />

Neither Spring STS, nor Eclipse show any warnings or errors in my bean files. I reference the imported beans all over the place. The Maven build works fine and the DAO integration tests all pass (they use the beans). I don't have any service integration tests yet.

But when I start up the WAR in Jetty I get an error:

Error creating bean with name 'securityService' 
Cannot resolve reference to bean 'userDAO' while setting constructor argument

All of the imported bean XML files can be found inside their respective JAR files in the WEB-INF/lib directory. Indeed, the service bean that threw the error is itself defined inside the service.xml file inside the service module's JAR file.

Apparently the service module can't find the bean that it imported from the dao module. Obviously I don't understand something...seems like this should this Just Work?

like image 581
HDave Avatar asked Sep 23 '10 19:09

HDave


1 Answers

I enabled DEBUG logging for 'org.springframework' in order to see if I could learn anything. What I found were messages to the effect that the DAO beans had been created, but there was also a message about them having no name or id.

I check the file, and they all did have an id. So what was it? I check the XML namespace and saw:

http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"

and noticed it was old (I am using Spring 3.0.2) and changed it to:

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

Once I changed it, Spring instantly threw half a dozen errors regarding beans that were defined incorrectly (but never used apparently). Once I fixed those errors, everything Just Worked. I've since gone through the entire system checking Spring XML file namespace versions.

Thanks to all for the help. Can't believe I wasted a day on this stupidity!!

like image 143
HDave Avatar answered Oct 06 '22 13:10

HDave