Get up to speed with the core of Maven quickly, and then go beyond the foundations into the more powerful functionality of the build tool, such as profiles, scopes, multi-module projects and quite a bit more:
Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.
Get started with mocking and improve your application tests using our Mockito guide:
Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.
Get started with understanding multi-threaded applications with our Java Concurrency guide:
Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:
Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.
But these can also be overused and fall into some common pitfalls.
To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:
Get started with Spring and Spring Boot, through the Learn Spring course:
>> LEARN SPRINGExplore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:
Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.
I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.
You can explore the course here:
Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.
Get started with Spring Data JPA through the guided reference course:
Refactor Java code safely β and automatically β with OpenRewrite.
Refactoring big codebases by hand is slow, risky, and easy to put off. Thatβs where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.
Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions β one for newcomers and one for experienced users. Youβll see how recipes work, how to apply them across projects, and how to modernize code with confidence.
Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.
Table of Contents
- Overview
- Basic Spring Dependencies with Maven
- Spring Persistence with Maven
- Spring MVC with Maven
- Spring Security with Maven
- Spring Test with Maven
- Using Milestones
- Using Snapshots
- Conclusion
1. Overview
This tutorial illustrates how to set up the Spring dependencies via Maven. The latest Spring releases can be found on Maven Central.
2. Basic Spring Dependencies With Maven
Spring is designed to be highly modular β using one part of Spring should not and does not require another. For example, the basic Spring Context can be without the Persistence or the MVC Spring libraries.
Letβs start with a fundamental Maven setup which will only use the spring-context dependency:
<properties>
<org.springframework.version>5.2.8.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 weβre defining the dependency in the runtime scope β this will make sure that there are no compile-time dependencies on any Spring specific APIs. For more advanced use cases, 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 JDK 8 is the minimum Java version required for Spring 5.2. It also supports JDK 11 as the current LTS branch and JDK 13 as the latest OpenJDK release.
3. Spring Persistence With Maven
Letβs now look at the persistence Spring dependencies β mainly spring-orm:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
This comes with Hibernate and JPA support β such as HibernateTemplate and JpaTemplate β as well as 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.
4. Spring MVC With Maven
To use the Spring Web and Servlet support, two dependencies 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.
Starting Spring 5.0, for the reactive-stack web framework support, we can add the dependency for Spring WebFlux:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<version>${org.springframework.version}</version>
</dependency>
5. Spring Security With Maven
Security Maven dependencies are discussed in depth in the Spring Security with Maven article.
6. Spring Test With Maven
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>
With Spring 5, we can perform concurrent test execution as well.
7. Using Milestones
The release version of Spring is 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://repo.spring.io/milestone/</url>
</repository>
</repositories>
Once this repository has been defined, the project can define dependencies such as:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.0-M1</version>
</dependency>
8. Using Snapshots
Similar to milestones, snapshots are hosted in a custom repository:
<repositories>
<repository>
<id>repository.springframework.maven.snapshot</id>
<name>Spring Framework Maven Snapshot Repository</name>
<url>http://repo.spring.io/snapshot/</url>
</repository>
</repositories>
Once the SNAPSHOT repository is enabled in the pom.xml, the following dependencies can be referenced:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.3.BUILD-SNAPSHOT</version>
</dependency>
As well as β for 5.x:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.3.0-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 several others 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.
