zoukankan      html  css  js  c++  java
  • Spring mvc 4系列教程(二)——依赖管理(Dependency Management)和命名规范(Naming Conventions)

    依赖管理(Dependency Management)和命名规范(Naming Conventions

    依赖管理和依赖注入(dependency injection)是有区别的。为了将Spring的优秀特性(如依赖注入)带到你的应用中,需要在编译时或运行时部署所需要的库(jar包)。这些依赖不是虚拟的构件,而是文件系统上的物理资源。依赖管理的过程涉及到定位这些资源、存储资源、加入classpath。依赖可以是直接的(例如Spring运行时),也可以是间接的(例如commons-dbcp)。间接的依赖(也可以说是transitive)很难标识和管理。

    如果你要使用Spring,首先需要拷贝相应的jar包。为了方便使用,Spring将不同的依赖按模块进行了封装,例如,如果你正在开发的是一个非web应用,可以不必引用sping-web模块。可以通过命名规范spring-* 或 spring-*.jar引用sping库。*表示模块的简称(例如spring-core,spring-webmvc,spring-jms)。实际的jar包后面一般会有版本串号(例如spring-core-4.1.1.RELEASE.jar)。

    对于依赖的管理,建议使用Maven,Gradle 或 Ivy管理,当然jar包还是需要手动自己下载的。

    Spring依赖

    虽然Spring为大范围的企业应用和外部工具提供了集成和支持,但强制依赖很少(例如,没必要为了开发一个简单的Spirng用例,而去定位和下载大量的jar包)。在其中的依赖注入中,只有一个外部依赖是强制的,即日志(log)。

    接下来将会概述Spring的配置步骤。不管在什么场景下,如有不清楚的问题,请参考依赖管理文档,或者查看Spring样例代码。Spring本身是使用Gradle来管理依赖,但后文将会使用Gradle或者Maven

    1.Maven依赖管理

    如果你使用Maven进行依赖管理,不需要显式提供日志依赖。例如,如果要创建应用上下文(context),则Maven的依赖注入配置如下:

    <dependencies>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    <version>4.1.1.RELEASE</version>

    <scope>runtime</scope>

    </dependency>

    </dependencies>

    需要注意的是,如果不想与Spring API一起编译,则scopes可以声明为runtime

    使用Spring Maven资源还需要在配置文件中指定资源位置。

    <repositories>

    <repository>

    <id>io.spring.repo.maven.release</id>

    <url>http://repo.spring.io/release/</url>

    <snapshots><enabled>false</enabled></snapshots>

    </repository>

    </repositories>

    1.1 Bill Of Materials

    有时我们会在不经意间引用不同版本的jar包。例如,有时从第三方或其它项目导入不同版本的jar包,如果不显式声明你的直接依赖,可能会产生很多诡异的问题。

    为了解决此问题,Maven支持BOM(bill of materials)的概念。可以在dependencyManagement里面导入spring-framework-bom,以确保所有的依赖(包括直接的和间接的)引用的都是同一版本。

    <dependencyManagement>

    <dependencies>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-framework-bom</artifactId>

    <version>4.1.1.RELEASE</version>

    <type>pom</type>

    <scope>import</scope>

    </dependency>

    </dependencies>

    </dependencyManagement>

    使用BOM的另一好处是不用指定版本号。

    <dependencies>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context</artifactId>

    </dependency>

    <dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-web</artifactId>

    </dependency>

    <dependencies>

  • 相关阅读:
    Merge sorted ranges
    call_once/once_flag的使用
    对‘boost::serialization::singleton_module::get_lock()’未定义的引用
    C++多线程lock_guard
    长度为0的数组—— Arrays of Length Zero
    Utunbu VLC 播放器播放本机rtp码流
    Utunbu VLC 播放器播放本机h264码流
    Declaration of non-local variable in 'for' loop
    ZFEC--Demo--C语言接口
    malloc-demo
  • 原文地址:https://www.cnblogs.com/jpcflyer/p/5811920.html
Copyright © 2011-2022 走看看