依赖管理
Maven Dependency Management
Maven "Bill of Materials" Dependency:
使用Spring的不同模块时,可能由于使用不同版本的Spring模块引入同一第三方库的不同版本。
Maven 通过 “BOM” 来解决这个问题:在dependencyManagement中引入spring-framework-bom来确保所有的spring模块都使用统一的版本:
<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>
此时,所有使用的spring模块dependency都不需要在声明version属性。例如:
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependencies>
(只有当外层的dependency元素中没有指明版本信息时,dependencyManagement中的dependencies元素才会起作用)。
Logging
Spring默认使用Jakarta Commons Logging API(JCL)作为logging framework。
不使用Commons Logging
从spring-core模块中移除掉commons-logging,
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
移除commons-logging后,我们需要添加一个替代的logging framework。
使用SLF4J
SLF4J可以绑定到许多的logging框架,同时也提供了反向操作,桥接其他的logging 框架到SLF4J.
因此,在Spring中使用SLF4J需要使用SLF4J-JCL bridge去替代commons-logging依赖,Spring中的logging调用将会转换成SLF4J API的调用。
需要提供4个dependencies(同时exclude commons-logging):the bridge, the slf4j api, the binding to log4j, the log4j implementation:
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>