title: Multi module Spring-Hibernate-Maven web application, spring not resolving entities/daos
body: <p>I've got a strange problem which I think is Spring related. I'm developing a Maven multi module application structured as follow:</p>
  
  <ul>
  <li>DataModule (includes generale Hibernate entities and daos)</li>
  <li>ServiceModule (includes specific Hibernate entities and daos) - depends on DataModule</li>
  <li>Web (gwt web module) - depends on ServiceModule</li>
  </ul>
  
  <p>In <strong>DataModule</strong> I've a classic Spring-Hibernate xml configuration, consisting of (relevant code):</p>
  
  <p><strong>ApplicationContext.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans ..&gt;
  
  &lt;!-- Auto scan the components --&gt;
  &lt;context:annotation-config /&gt;
  &lt;context:component-scan base-package="com.x.dataModule" &gt;
  
  &lt;context:include-filter type="regex" 
                     expression="com.x.dataModule.dao.*" /&gt;
  
  &lt;/context:component-scan&gt;
  
  &lt;!-- Import resources --&gt;
  &lt;import resource="applicationContext-hibernate.xml" /&gt;
  &lt;import resource="applicationContext-dataSource.xml" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>applicationContext-hibernate.xml</strong></p>
  
  <pre><code>&lt;beans ..&gt;
  
  &lt;bean id="defaultLobHandler"  class="org.springframework.jdbc.support.lob.DefaultLobHandler" /&gt;
  
  &lt;!-- Hibernate session factory --&gt;
  &lt;bean id="sessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean "&gt;
  
  &lt;property name="packagesToScan" value="com.x.dataModel.model.**.*"&gt;&lt;/property&gt; 
      &lt;property name="configLocation" value="classpath:hibernate.cfg.xml" /&gt;
      &lt;property name="lobHandler" ref="defaultLobHandler" /&gt;
      &lt;property name="dataSource" ref="dataSource" /&gt;
  
      &lt;property name="hibernateProperties"&gt;
          &lt;props&gt;
              &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.MySQLDialect&lt;/prop&gt;
              &lt;prop key="hibernate.query.substitutions"&gt;true 1, false 0&lt;/prop&gt;
              &lt;prop key="hibernate.connection.useUnicode"&gt;true&lt;/prop&gt;
              &lt;prop key="hibernate.connection.charSet"&gt;UTF8&lt;/prop&gt;
          &lt;/props&gt;
      &lt;/property&gt;
  
  &lt;/bean&gt;
  
  &lt;!-- Transaction Manager --&gt;
  &lt;bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
      &lt;property name="sessionFactory" ref="sessionFactory" /&gt;
  &lt;/bean&gt;
  
   &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; 
  
  
  
  &lt;bean id="persistenceExceptionTranslationPostProcessor"
  class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;
  
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Hibernate.cfg is actually empty, since I aim to avoid explicit mapping due the use of <strong>packagesToScan</strong> property. applicationContext-dataSource is not relevant since it contains ds configuration.</p>
  
  <p>The <strong>ServiceModule</strong> relies on the previous module.</p>
  
  <p><strong>applicationContext.xml</strong></p>
  
  <pre><code>&lt;beans ...&gt;
  
  &lt;!-- Auto scan the components --&gt;
  </code></pre>
  
  <p>
    </p>
  
  <pre><code>&lt;context:include-filter type="regex" 
                     expression="com.x.serviceModule.dao.*" /&gt;
  
  &lt;context:include-filter type="regex" 
                     expression="com.x.serviceModule.manager.*" /&gt;
  </code></pre>
  
  <p></p>
  
  <pre><code>&lt;!-- Import resources --&gt;
  &lt;context:property-override location="classpath:override.properties"/&gt;
  &lt;import resource="classpath*:/applicationContext-hibernate.xml" /&gt;
  &lt;import resource="classpath*:/applicationContext-dataSource.xml" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>The file <strong>override.properties</strong> contains the following entry, to modify the definition of <strong>packagesToScan</strong></p>
  
  <p><code>sessionFactory.packagesToScan=com.x.*.model.**.*</code></p>
  
  <p>At this point everything works just fine. The serviceManagers (singletons) are loaded from the factory as</p>
  
  <pre><code>ApplicationContext appContext = new ClassPathXmlApplicationContext(
                  "applicationContext.xml").getBean(..);
  </code></pre>
  
  <p>And everything is behaving as expected.</p>
  
  <p>Last part, the <strong>Web Module</strong>. If I import the ServiceModule and test it as a Java application, again everything is fine. <strong>However, if I start the module as a web application, it fails complaining that it's not able to load entities/daos defined in the DataModule.</strong></p>
  
  <p>Relevant files in Web Module:</p>
  
  <p>WEB-INF/spring
  applicationContext.xml: nothing here other than the import of applicationContext-security.xml (residing in the same directory)</p>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code>&lt;!-- Spring context --&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;
          WEB-INF/spring/applicationContext.xml
      &lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;!-- Creates the Spring Container --&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.security.web.session.HttpSessionEventPublisher&lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p><strong>The only way to avoid the problem is to declare explicitly the entities mapping and daos definition in the DataModule xml files</strong>.</p>
  
  <p>My question: is there a way to avoid such declarations, and rely entirely on the component/package scan also in Web Module?</p>
  
  <p>I'm not really sure but looks like that somehow the spring web context is conflicting with my other spring context. Otherwise, I don't understand why everything is fine as Java application, but fails as web.
  Also (not sure if relevant) it seems that the Session Factory bean is istantiated twice during the process (shouldn't it be a singleton?).</p>
  
  <p>Spring version is 3.1.1 Release, Hibernate 3.6.0 Final (4.0.1 for commons-annotations). Tomcat 7 for deploy.</p>
  
  <p>I didn't provide the Java code of entities/daos/managers since I don't think it's relevant for the question; however, I can provide it if it's of any help. Thanks a lot</p>