Spring with Maven – 用Maven开发Spring

最后修改: 2013年 4月 17日

中文/混合/英文(键盘快捷键:t)

Table of Contents

目录

  1. Overview
  2. Basic Spring Dependencies with Maven
  3. Spring Persistence with Maven
  4. Spring MVC with Maven
  5. Spring Security with Maven
  6. Spring Test with Maven
  7. Using Milestones
  8. Using Snapshots
  9. Conclusion

1. Overview

1.概述

This tutorial illustrates how to set up the Spring dependencies via Maven. The latest Spring releases can be found on Maven Central.

本教程说明了如何通过Maven设置Spring的依赖关系。最新的Spring版本可以在Maven Central上找到。

2. Basic Spring Dependencies With Maven

2.使用Maven的基本Spring依赖性

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.

Spring的设计是高度模块化的–使用Spring的一个部分不应该也不需要另一个部分。例如,基本的Spring Context可以没有Persistence或MVC Spring库。

Let’s start with a fundamental Maven setup which will only use the spring-context dependency:

让我们从基本的Maven设置开始,它将只使用spring-context依赖性

<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.

这个依赖关系–spring-context–定义了实际的Spring注入容器,有少量的依赖关系。spring-corespring-expressionspring-aopspring-beans。这些技术通过支持一些核心 Spring 技术来增强容器:Core Spring 实用程序、Spring Expression Language(SpEL)、面向前景的编程支持和JavaBeans 机制

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.

请注意,我们是在runtime范围内定义依赖关系的–这将确保对任何Spring特定的API没有编译时的依赖。对于更高级的用例,runtime范围可以从一些选定的Spring依赖中移除,但对于更简单的项目,不需要针对Spring进行编译以充分利用该框架。

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.

另外,请注意,JDK 8是Spring 5.2所需的最低Java版本。它还支持作为当前LTS分支的JDK 11和作为最新OpenJDK版本的JDK 13。

3. Spring Persistence With Maven

3.使用Maven的Spring持久性

Let’s now look at the persistence Spring dependencies – mainly spring-orm:

现在让我们来看看持久化的Spring依赖项–主要是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.

它带有对Hibernate和JPA的支持–如HibernateTemplateJpaTemplate–以及一些额外的、与持久性有关的依赖。spring-jdbcspring-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.

JDBC数据访问库定义了Spring JDBC支持以及JdbcTemplate,而spring-tx表示极其灵活的Transaction管理抽象

4. Spring MVC With Maven

4.使用Maven的Spring MVC

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:

要使用Spring Web和Servlet支持,除了上面的核心依赖外,还需要在pom中包含两个依赖。

<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.

spring-web依赖项包含了Servlet和Portlet环境下常见的Web专用工具,而spring-webmvc为Servlet环境提供MVC支持

Since spring-webmvc has spring-web as a dependency, explicitly defining spring-web is not required when using spring-webmvc.

由于spring-webmvcspring-web作为一个依赖项,在使用spring-web时不需要明确定义spring-web

Starting Spring 5.0, for the reactive-stack web framework support, we can add the dependency for Spring WebFlux:

从Spring 5.0开始,为了支持反应堆Web框架,我们可以添加Spring WebFlux的依赖性。

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

5. Spring Security With Maven

5.使用Maven的Spring安全

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

安全的Maven依赖性Spring Security with Maven文章中得到了深入讨论。

6. Spring Test With Maven

6.用Maven进行Spring测试

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

Spring测试框架可以通过以下依赖关系包含在项目中。

<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.

通过Spring 5,我们也可以执行并发测试执行

7. Using Milestones

7.使用里程碑

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:

Spring的发布版本托管在Maven中心。但是,如果项目需要使用里程碑版本,则需要在pom中添加自定义Spring仓库。

<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

8.使用快照

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:

一旦在pom.xml中启用了SNAPSHOT资源库,就可以引用以下依赖关系。

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

As well as – for 5.x:

以及–对于5.x。

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

9. Conclusion

9.结论

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.

本文讨论了Spring与Maven的实际使用细节。这里介绍的Maven依赖项当然是一些主要的依赖项,还有几个可能值得一提,但还没有入选。尽管如此,这应该是在项目中使用Spring的一个良好起点。