Spring -- 依赖管理

依赖管理和命名约定

依赖管理和依赖注入是两回事。要集成依赖注入功能的话,需要在运行时或编译时把所有lib(jar)放进classpath。这些依赖不是注入的虚拟组件,而是文件系统中的物理资源。依赖管理包括 找到这些资源,存储它们以及把它们添加到classpaths中。依赖可以是直接的(在运行时依赖Spring),也可以是间接的(依赖的commons-dhcp本身是依赖于commons-pool的),间接的依赖也叫做“transitive”,而且这些依赖不好识别和管理。

用Spring的话需要把相关的jar放在项目中,我们可以用速记的命名方式 spring-*spring-*.jar* 表示模块的简称(比如spring-corespring-webmavcspring-jms)。

每个Spring框架的release版本都会发布到Maven Central仓库和 Spring Maven仓库。
-

Maven依赖管理

如果使用Maven作为依赖管理,你不需要显示地提供logging依赖,比如,创建一个application context 并使用依赖注入配置一个应用,Maven的依赖配置如下:

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

注意,如果不想在编译时就注入的话,也可以在运行时声明这个依赖。
上面的这个例子使用的是Maven Central仓库,如果使用Spring Maven仓库的话,需要指定仓库的位置,下面是完全release的:

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

下面是snapshot的:

<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”依赖

使用Maven可以混合不同版本的Spring Jar包。比如你的第3方类库或者另一个Spring工程,引用的是一个老版本的release,如果忘记了显式地声明一个直接依赖,可能会出问题。
解决办法是支持一个叫做“Bill of Materials”概念的依赖,在dependencyManagement 字段中导入 spring-framework-bom,确保所有spring依赖都是同样的版本。

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

用BoM的另外一个好处是:依赖Spring框架时你不再需要指定< version > 属性。

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

Gradle依赖管理

要在Gradle构建项目时使用Spring仓库时,这样:

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

也可以把 release 换成 milestonesnapshot,在仓库指定好后,就可以声明依赖了:

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

使用zip压缩包本地依赖

虽然使用构建系统的依赖管理是推荐方式,也可以把依赖包下载到本地。依赖包都在Spring Maven Repository中,http://repo.spring.io/release/org/springframework/spring

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值