Method steps for configuring Spring using Maven

  • 2021-07-24 11:04:44
  • OfStack

This article explains how to configure Spring dependencies through Maven. The latest version of Spring can be found on Maven Central.

Spring Basic Dependencies in Maven

The design of Spring is highly modular-using part 1 of Spring should not and does not require another part. For example, the basic Spring Context may have no Persistence or MVC Spring libraries.

Let's start with a basic Maven configuration that will only use spring-context dependencies:


<properties>
  <org.springframework.version>3.2.8.RELEASE</org.springframework.version>
  <!-- <org.springframework.version>4.0.2.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. The container is extended by supporting one core Spring technology: Core Spring utility, Spring expression language (SpEL), object-oriented programming support, and JavaBeans mechanism.

Note that we define dependencies in the runtime scope-this ensures that there are no compile-time dependencies on any API specific to Spring. For more advanced use cases, the runtime scope can be removed from one of the selected Spring dependencies, but for simpler projects, there is no need to compile against Spring to take full advantage of the framework.

Also note that starting with Spring 3.2, there is no need to define an CGLIB dependency (now upgraded to CGLIB 3.0)-it has been repackaged (all net. sf. cglib packages are now org. springframework. cglib) and directly inline spring-core JAR internally (see JIRA for additional details).

Maven Configuration Spring Persistence

Now let's look at the Spring Persistence 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-and one additional persistence-related dependency: spring-jdbc and spring-tx.

The JDBC data access library defines Spring JDBC support as well as JdbcTemplate, while spring-tx represents an extremely flexible transaction management abstraction.

Maven Configuration Spring MVC

To use Spring Web and Servlet support, you need to include two dependencies in pom in addition to the core dependencies 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 the Servlet and Portlet environments, while spring-webmvc supports MVC for the Servlet environment.

Because spring-webmvc has spring-web as a dependency, there is no need to explicitly define spring-web when using spring-webmvc.

Configuring Spring Test with Maven

Spring Test Framework can be included in a project through the following dependencies:


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

Beginning with Spring 3.2, the Spring MVC Test project has been included in the core test framework-so it is sufficient to include the spring-test dependencies.

Using Milestones

The release version of Spring is hosted on Maven Central. However, if your project needs to use the Milestones version, you need to add a custom Spring repository to 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>

One of this repositories has been defined, and the project can define dependencies, such as:


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

Using Snapshots

Like Milestones, Snapshots is 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>

With the SNAPSHOT repository enabled in pom. xml, the following dependencies can be referenced:


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

For 4. x:


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

Related articles: