4

I have 2 web projects in Spring MVC that are similar and have the same services and models. I want to split the code in order to have a common CORE.

I created a new project (CORE) with all the services and models that are shared and exported it as jar as specified here (Auto-wiring annotations in classes from dependent jars) but the components are not scanned and auto-wired in CHILD projects.

So, my questions are:

What are the other options besides jars to make the auto-wire happen?

What is the best practice to split code base and share the core components in spring projects?

Community
  • 1
  • 1
Miciurash
  • 4,844
  • 4
  • 15
  • 21

2 Answers2

2

Something like the below. See http://books.sonatype.com/mvnex-book/reference/multimodule.html for more info.

Maven parent module /pom.xml:

...
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>X.X-TAG</version>
<packaging>pom</packaging>
<modules>
<module>core</module>
<module>child1</module>
</modules>
...

Maven core module: /core/pom.xml:

...
<parent>
<artifactId>parent</artifactId>
<groupId>com.example</groupId>
<version>X.X-TAG</version>
</parent>
<packaging>jar</packaging>
<artifactId>core</artifactId>
...

Maven child1 module: /child1/pom.xml:

...
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>X.X-TAG</version>
</parent>
<packaging>war</packaging>
<artifactId>child1</artifactId>
<dependencies>
<dependency>
    <groupId>${parent.groupId}</groupId>
    <artifactId>core</artifactId>
    <version>${parent.versionId}</version>
</dependency>
</dependencies>
...
Neil McGuigan
  • 43,981
  • 12
  • 119
  • 145
0

I would use cross-context. This way you could autowire your beans in many applications having this module separated from the others and, of course, the code you want to share between applications won't be duplicated.

I advise to take a look at this guide:

http://blog.imaginea.com/cross-context-communication-between-web-applications/

From Spring blog: http://spring.io/blog/2007/06/11/using-a-shared-parent-application-context-in-a-multi-war-spring-application/

Luke SpringWalker
  • 1,570
  • 3
  • 16
  • 32