alexgrimaldi
alexgrimaldi

Reputation: 303

Spring MVC in a multi maven module project

I am designing and implementing a Java web application using Spring, Spring MVC and Maven. My goal is to create three modules:

Communications between the service module and the data module work great. The service module depends on the data module and it includes the data module with <import resource="dataApplicationContext.xml"/> in serviceApplicatioContext.xml. I have tested it with a static main class and some integration tests.

The problem I am having is about making the web module to depend on the service module. How do I make my mvc-dispatcher-servlet.xml import my serviceApplicatioContext.xml?

Solution I have tried so far: 1)If I just use <import resource="/serviceApplicationContext.xml"/> then Spring MVC would look for /WEB-INF/serviceApplicationContext.xml which, of course, does not exist.

2)If I replace <context:component-scan base-package="my.project.controller" /> with <context:component-scan base-package="my.project" /> then Spring does not kick in and no hibernate session is created.

Thanks a lot, Alex

Upvotes: 2

Views: 3268

Answers (1)

M. Deinum
M. Deinum

Reputation: 125232

Spring has its own resource loading mechanism and each implementation of an ApplicationContext has its own default resource location. The ClassPathXmlApplicationContext by defaults loads from the classpath, whereas the XmlWebApplicationContext loads from the web application root.

You can specify on each resource where to load it from by prefixing it. classpath: leads to always loading from the classpath, file: from the file-system etc.

In short simply add classpath: to the name of the resources to import to force that this file is always loaded from the classpath.

<import resource="classpath:serviceApplicationContext.xml" />

Upvotes: 2

Related Questions