Spring Dependencies and Depending on Spring

来源:互联网 发布:从其他excel导入数据 编辑:程序博客网 时间:2024/06/09 10:00

Spring的依赖和被依赖

Although Spring provides integration and support for a huge range of enterprise and other external tools, it intentionally keeps its mandatory dependencies to an absolute minimum: you shouldn’t have to locate and download (even automatically) a large number of jar libraries in order to use Spring for simple use cases. For basic dependency injection there is only one mandatory external dependency, and that is for logging (see below for a more detailed description of logging options).


虽然Spring为大量企业和其他扩展工具提供了集成以及支持,有意识的将他的依赖性保持在最低的程度:有不必因为要使用Spring做简单的用例,来定位以及下载(自动)大量的jar包。对于基本的依赖注入。只有一个强制的外部依赖关系,也就是下文中的日志记录。


Next we outline the basic steps needed to configure an application that depends on Spring, first with Maven and then with Gradle and finally using Ivy. In all cases, if anything is unclear, refer to the documentation of your dependency management system, or look at some sample code - Spring itself uses Gradle to manage dependencies when it is building, and our samples mostly use Gradle or Maven.


接下来,我们概述下配置Spring 依赖的步骤,首先是使用Maven,然后是 Grable最后是lvy。在任何情况下,如果有任何不清的,请参看请使用的依赖管理系统的文档,或者查看一些示例代码,Spring 本身使用Gradle管理依赖,我们的示例中大多数使用Gradle和Maven。


Maven Dependency Management(使用Maven进行依赖管理)

If you are using Maven for dependency management you don’t even need to supply the logging dependency explicitly. For example, to create an application context and use dependency injection to configure an application, your Maven dependencies will look like this:


如果你使用Maven进行依赖管理,你甚至不需要明确地提供日志管理的依赖。例如,要创建应用程序的上下文,并使用依赖注册来配置你的应用程序,你的Maven依赖将会如下所示。

<dependencies>    <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-context</artifactId>        <version>5.0.0.BUILD-SNAPSHOT</version>        <scope>runtime</scope>    </dependency></dependencies>

That’s it. Note the scope can be declared as runtime if you don’t need to compile against Spring APIs, which is typically the case for basic dependency injection use cases.


那么,需要注意的是如果不需要编译Spring Api 那么可以将Scope 设置未 runtime,这是基本的依赖注入用例

The example above works with the Maven Central repository. To use the Spring Maven repository (e.g. for milestones or developer snapshots), you need to specify the repository location in your Maven configuration. For full releases:


上面的例子应用于使用Maven Central的情况下,要使用Spring Maven 库的时候,你需要在Maven中配置仓库的地址

<repositories>    <repository>        <id>io.spring.repo.maven.release</id>        <url>http://repo.spring.io/release/</url>        <snapshots><enabled>false</enabled></snapshots>    </repository></repositories>

对于 milestones:

<repositories>    <repository>        <id>io.spring.repo.maven.milestone</id>        <url>http://repo.spring.io/milestone/</url>        <snapshots><enabled>false</enabled></snapshots>    </repository></repositories>

对于 snapshots:

<repositories>    <repository>        <id>io.spring.repo.maven.snapshot</id>        <url>http://repo.spring.io/snapshot/</url>        <snapshots><enabled>true</enabled></snapshots>    </repository></repositories>

Maven “Bill Of Materials” Dependency

It is possible to accidentally mix different versions of Spring JARs when using Maven. For example, you may find that a third-party library, or another Spring project, pulls in a transitive dependency to an older release. If you forget to explicitly declare a direct dependency yourself, all sorts of unexpected issues can arise.


使用Maven时,可能会意外的混淆不同版本的Spring Jar文件,例如,你可能会发现一个三方的库或者另一个Spring项目 依赖一个旧的版本,如果你忘记了声明版本,那么可能会出现各种意想不到的结果。

To overcome such problems Maven supports the concept of a “bill of materials” (BOM) dependency. You can import the spring-framework-bom in your dependencyManagement section to ensure that all spring dependencies (both direct and transitive) are at the same version.


为了克服这些问题,Maven支持BOM 概念,你可以将spring-framework-bom 导入到dependencyManagement中,以确定所有的依赖版本都是相同的

<dependencyManagement>    <dependencies>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-framework-bom</artifactId>            <version>5.0.0.BUILD-SNAPSHOT</version>            <type>pom</type>            <scope>import</scope>        </dependency>    </dependencies></dependencyManagement>

An added benefit of using the BOM is that you no longer need to specify the attribute when depending on Spring Framework artifacts:

使用BOM的另一个好处时 你不在需要指定Spring Framework 组件的版本了。

<dependencies>    <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-context</artifactId>    </dependency>    <dependency>        <groupId>org.springframework</groupId>        <artifactId>spring-web</artifactId>    </dependency><dependencies>

Gradle Dependency Management
Gradle 依赖管理

To use the Spring repository with the Gradle build system, include the appropriate URL in the repositories section:


使用Gradle构建系统来构建Spring的资源库,你需要在repositories部分添加相应的URI

repositories {    mavenCentral()    // and optionally...    maven { url "http://repo.spring.io/release" }}

You can change the repositories URL from /release to /milestone or /snapshot as appropriate. Once a repository has been configured, you can declare dependencies in the usual Gradle way:


可以根据需要把repositories URL中的/release修改为/milestone或/snapshot。一旦仓库配置好了,就可以按Gradle的方式声明依赖关系了

dependencies {    compile("org.springframework:spring-context:5.0.0.BUILD-SNAPSHOT")    testCompile("org.springframework:spring-test:5.0.0.BUILD-SNAPSHOT")}

Ivy Dependency Management


使用lvy进行依赖管理

If you prefer to use Ivy to manage dependencies then there are similar configuration options.


如果你喜欢使用lvy来进行依赖管理,那么也有类似的配置选项。

To configure Ivy to point to the Spring repository add the following resolver to your ivysettings.xml:

要配置lvy需要将一下的解析器添加到 ivysettings.xml 中来指向Spring库。

<resolvers>    <ibiblio name="io.spring.repo.maven.release"            m2compatible="true"            root="http://repo.spring.io/release/"/></resolvers>

You can change the root URL from /release/ to /milestone/ or /snapshot/ as appropriate.

Once configured, you can add dependencies in the usual way. For example (in ivy.xml):


可以根据需要把root URL中的/release修改为/milestone或/snapshot。一旦仓库配置好了,就可以按lvy的方式声明依赖关系了


<dependency org="org.springframework"
name="spring-core" rev="5.0.0.BUILD-SNAPSHOT" conf="compile->runtime"/>

Distribution Zip Files

— zip发行版本

Although using a build system that supports dependency management is the recommended way to obtain the Spring Framework, it is still possible to download a distribution zip file.


尽管我们推荐使用构建系统来获取Sping Framework,但是我们也提供了Zip发行版。

Distribution zips are published to the Spring Maven Repository (this is just for our convenience, you don’t need Maven or any other build system in order to download them).


zip发行版本已经发布到了 Spring Maven库中了。

To download a distribution zip open a web browser to http://repo.spring.io/release/org/springframework/spring and select the appropriate subfolder for the version that you want. Distribution files end -dist.zip, for example spring-framework-{spring-version}-RELEASE-dist.zip. Distributions are also published for milestones and snapshots.

浏览器中打开http://repo.spring.io/release/org/springframework/spring ,根据版本选择相应的目录,发行版以-dist.zip 结束,例如 pring-framework-{spring-version}-RELEASE-dist.zip 。milestones 和 snapshots也有相应的zip发行本。

maven pom 示例

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 煮的牛肉太硬怎么办 在产蛋鸡体重偏轻怎么办 产蛋鸡不上高峰怎么办 蛋鸡天热下蛋少怎么办 成年鸡嗉子鼓涨怎么办 黄牛拼失败了钱怎么办 磁力泥粘衣服上怎么办 鼻涕泥弄衣服上怎么办 水晶橡皮泥弄在衣服上怎么办 橡皮泥弄到衣服上怎么办 橡皮泥弄在衣服上怎么办 磁力泥弄衣服上怎么办 苹果7p按键坏了怎么办 苹果手机内存满了怎么办 来微信消息手机上面没提示怎么办 苹果手机储存空间满了怎么办 苹果六储存满了怎么办视频 微信听筒没声音怎么办 苹果手机有电自动关机怎么办 白玉蜗牛生蛋了怎么办 农民工工资拖欠怎么办没有合同 公司拖欠员工工资没签合同怎么办 东京去大阪乘大巴行李怎么办? 在外手机没电了怎么办 被公司起诉我该怎么办 支付宝付款刷脸怎么办 考到了差的中学怎么办 中学考高中没考上怎么办 如果考中学没有考上那怎么办 摩托车牌京b牌照怎么办 老公网贷还不了怎么办 丈夫欠下的债妻子怎么办 丈夫偷妻子的钱怎么办 刷信用卡显示不允许降级交易怎么办 每次月经头几天下不来怎么办 邮箱和安全问题都忘记了怎么办 大疆air无人机芯片过热怎么办 脸上反复冒痘痘闭口粉刺怎么办? 手被火烧伤起泡怎么办 手被打火机烧了怎么办 小孩手被火烧了怎么办