title: Spring MVC annotation based Configuration for multimodule project
body: <p>I implemented two maven based independent web project implemented using Spring MVC, hibernate and Jax-RS.</p>
  <p>But my requirement changed and now I need to combine both the project as a sub project into another project which is our parent project. So I use maven multimodule configuration.</p>
  <h2>Project 1: Parent project</h2>
  <pre class="lang-maven prettyprint-override"><code>&lt;packaging&gt;pom&lt;/packaging&gt;
  &lt;modules&gt;
      &lt;module&gt;../child1&lt;/module&gt;
      &lt;module&gt;../child2&lt;/module&gt;
  &lt;/modules&gt;
  </code></pre>
  <h2>Child 1:</h2>
  <pre class="lang-maven prettyprint-override"><code>&lt;packaging&gt;jar&lt;/packaging&gt;
  &lt;parent&gt;
      &lt;groupId&gt;com.xyz.alpha&lt;/groupId&gt;
      &lt;artifactId&gt;parent&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;relativePath&gt;../parent&lt;/relativePath&gt;
  &lt;/parent&gt;
  </code></pre>
  <h2>Child 2:</h2>
  <pre class="lang-maven prettyprint-override"><code>&lt;packaging&gt;jar&lt;/packaging&gt;
  
  &lt;dependency&gt;
          &lt;groupId&gt;com.xyz.alpha&lt;/groupId&gt;
          &lt;artifactId&gt;child1&lt;/artifactId&gt;
          &lt;version&gt;2.0.2&lt;/version&gt;
  &lt;/dependency&gt;
  &lt;parent&gt;
      &lt;groupId&gt;com.xyz.alpha&lt;/groupId&gt;
      &lt;artifactId&gt;parent&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;relativePath&gt;../parent&lt;/relativePath&gt;
  &lt;/parent&gt;
  </code></pre>
  <p>But I need to configure project in Java in such a way that it will scan components of the parent and both the child project and execute project.
  Currently I have separate configuration for each project as:</p>
  <h2>AppIntializer.java</h2>
  <pre><code>public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
      @Override
      protected Class&lt;?&gt;[] getRootConfigClasses() {
          return new Class[] { AppConfig.class };
      }
   
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
          return null;
      }
   
      @Override
      protected String[] getServletMappings() {
          return new String[] { &quot;/&quot; };
      }
  }
  </code></pre>
  <h2>AppConfig.java</h2>
  <pre><code>@Configuration
  @EnableWebMvc
  @ComponentScan(basePackages = &quot;com.x.y&quot;)
  public class AppConfig extends WebMvcConfigurerAdapter{
       
      @Bean
      public ViewResolver viewResolver() {
          InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
          viewResolver.setViewClass(JstlView.class);
          viewResolver.setPrefix(&quot;/WEB-INF/views/&quot;);
          viewResolver.setSuffix(&quot;.jsp&quot;);
          return viewResolver;
      }
      
      @Override
      public void addCorsMappings(CorsRegistry registry) {
          registry.addMapping(&quot;/**&quot;);
      }
      
      @Override
      public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
          configurer.enable();
      }
      
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler(&quot;/resources/**&quot;).addResourceLocations(&quot;/resources/&quot;);
        
      }
  }
  </code></pre>
  <h2>HibernateConfiguration.java</h2>
  <pre><code>@Configuration
  @EnableTransactionManagement
  @ComponentScan({ &quot;com.x.y.configuration&quot; })
  @PropertySource(value = { &quot;classpath:application.properties&quot; })
  public class HibernateConfiguration {
   
      @Autowired
      private Environment environment;
   
      @Bean
      public LocalSessionFactoryBean sessionFactory() {
          LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
          sessionFactory.setDataSource(dataSource());
          sessionFactory.setPackagesToScan(new String[] { &quot;com.x.y.model&quot; });
          sessionFactory.setHibernateProperties(hibernateProperties());
          return sessionFactory;
       }
       
      @Bean
      public DataSource dataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName(environment.getRequiredProperty(&quot;jdbc.driverClassName&quot;));
          dataSource.setUrl(environment.getRequiredProperty(&quot;jdbc.url&quot;));
          dataSource.setUsername(environment.getRequiredProperty(&quot;jdbc.username&quot;));
          dataSource.setPassword(environment.getRequiredProperty(&quot;jdbc.password&quot;));
          return dataSource;
      }
       
      private Properties hibernateProperties() {
          Properties properties = new Properties();
          properties.put(&quot;hibernate.dialect&quot;, environment.getRequiredProperty(&quot;hibernate.dialect&quot;));
          properties.put(&quot;hibernate.show_sql&quot;, environment.getRequiredProperty(&quot;hibernate.show_sql&quot;));
          properties.put(&quot;hibernate.format_sql&quot;, environment.getRequiredProperty(&quot;hibernate.format_sql&quot;));
          properties.put(&quot;hibernate.hbm2ddl.auto&quot;, environment.getRequiredProperty(&quot;hibernate.hbm2ddl.auto&quot;));
          return properties;        
      }
       
      @Bean
      @Autowired
      public HibernateTransactionManager transactionManager(SessionFactory s) {
         HibernateTransactionManager txManager = new HibernateTransactionManager();
         txManager.setSessionFactory(s);
         return txManager;
      }
  }
  </code></pre>