温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

使用Maven怎么对Spring进行配置

发布时间:2021-05-19 15:57:39 来源:亿速云 阅读:152 作者:Leah 栏目:编程语言

这篇文章给大家介绍使用Maven怎么对Spring进行配置,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

Maven中的Spring基本依赖关系

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

让我们先从一个基本Maven配置,将只使用了spring-context依赖:

<properties>
  <org.springframework.version>3.2.8.RELEASE</org.springframework.version>
  <!-- <org.springframework.version>4.0.2.RELEASE</org.springframework.version> -->
</properties>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${org.springframework.version}</version>
  <scope>runtime</scope>
</dependency>

这个依赖项 - spring-context - 定义了实际的Spring Injection Container,并且有少量的依赖项:spring-core,spring-expression,spring-aop和spring-beans。通过支持一些核心Spring技术来扩充容器:Core Spring实用程序,Spring表达式语言(SpEL),面向对象编程支持和JavaBeans机制。

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

另请注意,从Spring 3.2开始,不需要定义CGLIB依赖项(现在已升级到CGLIB 3.0) - 它已被重新打包(所有net.sf.cglib包现在是org.springframework.cglib)并且直接在内部内联spring-core JAR(有关其他详细信息,请参阅JIRA)。

Maven配置Spring Persistence

现在让我们看一下Spring Persistence依赖关系 - 主要是spring-orm:

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

这附带了Hibernate和JPA支持 - 例如HibernateTemplate和JpaTemplate - 以及一些额外的,持久性相关的依赖项:spring-jdbc和spring-tx。

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

Maven配置Spring MVC

要使用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>

spring-web依赖项包含Servlet和Portlet环境的公共web特定实用程序,而spring-webmvc支持Servlet环境的MVC。

由于spring-webmvc将spring-web作为依赖项,因此在使用spring-webmvc时不需要明确定义spring-web。

使用Maven配置Spring Test

Spring Test Framework可以通过以下依赖项包含在项目中:

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

从Spring 3.2开始,Spring MVC Test项目已经包含在核心测试框架中 - 因此包括spring-test依赖就足够了。

使用Milestones

Spring的发布版本托管在Maven Central上。但是,如果项目需要使用Milestones版本,则需要将自定义Spring存储库添加到pom中:

<repositories>
  <repository>
    <id>repository.springframework.maven.milestone</id>
    <name>Spring Framework Maven Milestone Repository</name>
    <url>http://repo.spring.io/milestone/</url>
  </repository>
</repositories>

已定义了一个此存储库,该项目可以定义依赖项,例如:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>3.2.0.RC2</version>
</dependency>

使用Snapshots

与Milestones类似,Snapshots托管在自定义存储库中:

<repositories>
  <repository>
    <id>repository.springframework.maven.snapshot</id>
    <name>Spring Framework Maven Snapshot Repository</name>
    <url>http://repo.spring.io/snapshot/</url>
  </repository>
</repositories>

在pom.xml中启用SNAPSHOT存储库后,可以引用以下依赖项:

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

对于4.x:

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

关于使用Maven怎么对Spring进行配置就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI