Spring with Maven

1. Overview

This tutorial will discuss how to setup Spring with Maven and will go over specific usecases of using Spring dependencies. The latest Spring releases can be found on Maven Central.

2. Basic Maven Spring Dependencies

Spring was designed to be modular and flexible – the basic Spring container can be used in a variety of scenarios, without including any of the more advanced functionality that the framework has to offer. A very basic Maven setup will only have to use the spring-context dependency:

<properties>
    <org.springframework.version>3.2.4.RELEASE</org.springframework.version>
</properties>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${org.springframework.version}</version>
    <scope>runtime</scope>
</dependency>

This dependency – spring-context – defines the actual Spring Injection Container and has a small number of dependencies: spring-core, spring-expression, spring-aop and spring-beans. These augment the container by enabling support for some of the core Spring technologies: the Core Spring utilities, the Spring Expression Language (SpEL), the Aspect Oriented Programming support and the JavaBeans mechanism.

Note the runtime scope is used when defining the spring-context dependency – this will make sure that there are no compile time dependencies on any Spring specific APIs. For more advanced usecases the runtime scope may be removed from some selected Spring dependencies, but for simpler projects, there is no need to compile against Spring to make full use of the framework.

Also note that, starting with Spring 3.2, the CGLIB dependnecy (now upgraded to CGLIB 3.0) has been repackaged (the all net.sf.cglib package is now org.springframework.cglib) and inlined directly within the spring-core JAR (see the JIRA for additional details), so there is no need to define it explicitly.

3. Spring Persistence with Maven

In addition to the core Spring dependencies shown above, the principal Spring persistence dependency is spring-orm:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>${org.springframework.version}</version>
</dependency>

This brings in Hibernate and JPA support – such as HibernateTemplate and JpaTemplate – as well as the a few additional, persistence related dependencies: spring-jdbc and spring-tx. The JDBC Data Access library defines the Spring JDBC support as well as the JdbcTemplate, and spring-tx represents the extremely flexible Transaction Management Abstraction in Spring.

4. Spring MVC with Maven

To use the Spring Web and Servlet support, there are two dependencies that need to be included in the pom, again in addition to the core dependencies from above:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${org.springframework.version}</version>
</dependency>

The spring-web dependency contains common web specific utilities for both Servlet and Portlet environments, while spring-webmvc enables the MVC support for Servlet environments. Since spring-webmvc has spring-web as a dependency, explicitly defining spring-web is not required when using spring-webmvc.

5. Spring Security with Maven

Security Maven dependencies are discussed in depth in the Spring Security with Maven article.

6. Spring Test

The Spring Test Framework can be included in the project via the following dependency:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>${spring.version}</version>
    <scope>test</scope>
</dependency>

As of Spring 3.2, the Spring MVC Test project, which started as a standalone project available on github, has been included into the core Test Framework; this means that Spring 3.2 applications should simply use the spring-test dependency.

For applications still using Spring 3.1 and below, the older standalone Maven dependency still exists and can be used for almost identical results. The dependency is not on Maven Central however, so using it will require adding a custom repository to the pom of the project.

7. Using Milestones

The release version of Spring are hosted on Maven Central. However, if a project needs to use milestone versions, then a custom Spring repository needs to be added to the pom:

<repositories>
    <repository>
        <id>repository.springframework.maven.milestone</id>
        <name>Spring Framework Maven Milestone Repository</name>
        <url>http://maven.springframework.org/milestone</url>
    </repository>
</repositories>

One this repository has been defined, the project can define dependencies such as:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.2.0.RC2</version>
</dependency>

8. Using Snapshots

Similar to milestons, snapshots are hosted in a custom repository:

<repositories>
    <repository>
        <id>repository.springframework.maven.snapshot</id>
        <name>Spring Framework Maven Snapshot Repository</name>
        <url>http://maven.springframework.org/snapshot</url>
    </repository>
</repositories>

Once the SNAPSHOT repository is enabled in the pom, the following dependencies can be referenced:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>3.3.0.BUILD-SNAPSHOT</version>
</dependency>

And even:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.0.0.BUILD-SNAPSHOT</version>
</dependency>

9. Conclusion

This article discusses the practical details of using Spring with Maven. The Maven dependencies presented here are of course some of the major ones, and there are several others that may be worth mentioning and have not yet made the cut. Nevertheless this should be a good starting point for using Spring in a project.
 

Reference: Spring with Maven from our JCG partner Eugen Paraschiv at the baeldung blog.
Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


× eight = 32



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

15,153 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books