首页 > 代码库 > java框架-----spring框架------在自己的项目中如何用maven管理spring相关jar包

java框架-----spring框架------在自己的项目中如何用maven管理spring相关jar包

1.文章内容概述:

  spring框架是支持maven的,因为spring框架的所有模块打包而成的jar包以及spring所依赖的其他jar包都被存放了一份在maven的中央仓库中,如果你的项目使用maven进行管理,那么你就可以在你的项目中通过maven来引入你的项目所依赖的spring相关的jar包或其他依赖库。

2.spring框架中maven相关的东西:

  概述:使用maven管理spring相关的jar包,需要在pom.xml中配置groupId、artifactId之类的东西,只有在pom.xml中配置了这些东西,maven才会为你的项目查找maven的本地资源库--->中央仓库--->其他远程仓库,下载更新项目所依赖的相关jar包到你的本地仓库。

    •  groupId is org.springframework. 
    • ArtifactId见下表
GroupIdArtifactIdDescription

org.springframework

spring-aop

Proxy-based AOP support

org.springframework

spring-aspects

AspectJ based aspects

org.springframework

spring-beans

Beans support, including Groovy

org.springframework

spring-context

Application context runtime, including scheduling and remoting abstractions

org.springframework

spring-context-support

Support classes for integrating common third-party libraries into a Spring application context

org.springframework

spring-core

Core utilities, used by many other Spring modules

org.springframework

spring-expression

Spring Expression Language (SpEL)

org.springframework

spring-instrument

Instrumentation agent for JVM bootstrapping

org.springframework

spring-instrument-tomcat

Instrumentation agent for Tomcat

org.springframework

spring-jdbc

JDBC support package, including DataSource setup and JDBC access support

org.springframework

spring-jms

JMS support package, including helper classes to send and receive JMS messages

org.springframework

spring-messaging

Support for messaging architectures and protocols

org.springframework

spring-orm

Object/Relational Mapping, including JPA and Hibernate support

org.springframework

spring-oxm

Object/XML Mapping

org.springframework

spring-test

Support for unit testing and integration testing Spring components

org.springframework

spring-tx

Transaction infrastructure, including DAO support and JCA integration

org.springframework

spring-web

Web support packages, including client and web remoting

org.springframework

spring-webmvc

REST Web Services and model-view-controller implementation for web applications

org.springframework

spring-webmvc-portlet

MVC implementation to be used in a Portlet environment

org.springframework

spring-websocket

WebSocket and SockJS implementations, including STOMP support

3.在自己的项目中使用maven为我的项目添加spring相关jar包 

  概述:在自己的项目中使用maven添加项目所依赖的spring相关jar包有多种方法,各种方法的详细状况叙述如下 

   3.1方法一,直接添加maven中央仓库中的spring框架相应模块对应的jar包,具体配置方法如下: 

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId><!--见本文章第二部分内容-->
        <artifactId>spring-context</artifactId><!--见本文章中第二部分内容:表格-->
        <version>4.3.4.RELEASE</version><!--spring相关模块对应jar包的版本-->
        <scope>runtime</scope><!-- 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.-->
    </dependency>
</dependencies>

    3.2方法二,不从maven中央仓库下载,而是从其他远程仓库(这里指 Spring Maven repository也即spring网站)下载spring框架相关jar包到maven本地仓库,具体配置方法如下

概述: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. 

具体配置方法:如下,配置pom.xml文件

pom.xml

For full releases:

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

For milestones:

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

And for snapshots:

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

  3.3方法三,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.

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.3.4.RELEASE</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 <version> attribute when depending on Spring Framework artifacts:

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

  第三部分内容小结:通过上面所述内容我们可以发现,使用maven给我们的软件项目添加spring各个模块对应的jar包以及spring所依赖的jar包共有两种方法,第一种方法就是将pom.xml配置成从maven中央仓库来下载spring相关jar包到maven本地仓库,这时主要是需要配置groupId、artifactId之类的元素;第二种方法就是将pom.xml配置成从spring远程仓库而非maven的中央仓库来进行管理spring相关jar包,从spring远程仓库下载而不是从maven中央仓库下载项目所依赖的spring相关jar包到计算机maven本地仓库。使用上述两种方法管理项目所依赖的spring相关jar包都是可以的,但是无论使用上述哪种方法,都要注意spring各个模块的jar包的版本冲突问题,譬如很可能你的spring-context-4.3.4.RELEASE.jar,但是spring-web模块相关jar包就配置成了3.4.3.RELEASE.jar,版本不一致,有可能会导致运行过程出错,为了避免spring不同模块之间版本不一致的问题,可以使用本章节第三小节:“ 3.3方法三,Maven "Bill Of Materials" Dependency”中所述方法配置pom.xml

java框架-----spring框架------在自己的项目中如何用maven管理spring相关jar包