【Spring.Framework】依赖管理, Logging

依赖管理


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>








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值