让 Spring Framework 依赖 SLF4J 的 Maven 配置

Spring Framework 一直以来都是依赖 commons-logging,经过在 Maven pom.xml 进行配置,可让 Spring Framework 依赖于愈来愈流行的 SLF4J,这是利用了 slf4j.org 提供的 jcl-over-slf4j 把 commons-logging API 转接到 SLF4J API 上实现的,这不就是移花接木吗?spring

1. 让 spring-context 排除对 commons-logging 的依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring-version}</version>
    <scope>runtime</scope>
    <exclusions>
	<exclusion>
	    <artifactId>commons-logging</artifactId>
	    <groupId>commons-logging</groupId>
	</exclusion>
    </exclusions>
</dependency>

2. 添加 slf4j-api 和 jcl-over-slf4j 配置

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>${slf4j-version}</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>${slf4j-version}</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>${slf4j-version}</version>
    <scope>runtime</scope>
</dependency>

slf4j-simple 是一个简单的 SLF4J API 实现,它直接向 System.err 输出日志内容,适用于简单应用。api

补充说明:目前 Spring Framework 的最新版本是 4.0.0.RELEASE,SLF4J 的最新版本是 1.7.5。spa

3. SLF4J 官方的“移花接木”方案图

 

相关文章
相关标签/搜索