Hibernate-annotations 3.4.0.GA和slf4j?

u12*_*123 4 eclipse maven-2 hibernate slf4j

我有一个使用hibernate-annotations 3.4.0.GA的maven项目A,它使用slf4j-api 1.5.5版(通过pom.xml文件中的依赖树检查).进一步的项目A将slf4j-log4j12版本1.4.2指定为依赖项.

我有另一个依赖项目A的maven项目B.在项目BI中指定了以下依赖项:

slf4j-api version 1.6.1,
logback-core version 0.9.24
logback-classic version 0.9.24
Run Code Online (Sandbox Code Playgroud)

从命令行使用maven构建良好.但是当我从eclipse启动配置运行项目时,我得到:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/mm/.m2/repository/org/slf4j/slf4j-log4j12/1.4.2/slf4j-log4j12-1.4.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/mm/.m2/repository/ch/qos/logback/logback-classic/0.9.24/logback-classic-0.9.24.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: slf4j-api 1.6.x (or later) is incompatible with this binding.
SLF4J: Your binding is version 1.5.5 or earlier.
SLF4J: Upgrade your binding to version 1.6.x. or 2.0.x
Run Code Online (Sandbox Code Playgroud)

从这条消息中我指出我需要将项目A中的绑定升级到1.6.x,但是我没有看到它是如何可能的,因为它包含在hibernate依赖项中.

是否可以在运行项目B时切换绑定(更新类路径信息),以便它使用1.6.1版本而不是hibernate项目中的版本?

Pas*_*ent 9

我有一个使用hibernate-annotations 3.4.0.GA的maven项目A,它使用slf4j-api 1.5.5版(通过pom.xml文件中的依赖树检查).进一步的项目A将slf4j-log4j12版本1.4.2指定为依赖项.

不建议这样做,你应该使用相同版本的slf4j工件.来自FAQ:

SLF4J版本是否向后兼容?

除了罕见的理论上的例外,SLF4J版本是向后兼容的.这意味着您可以从SLF4J 1.0版升级到任何更高版本而不会出现问题.

然而,当SLF4J API是从客户的角度来看非常稳定,SLF4J绑定,例如SLF4J-简单或SLF4J-log4j12可能需要SLF4J-API的特定版本.混合不同版本的slf4j工件可能会有问题,因此强烈建议不要这样做.举例来说,如果你正在使用SLF4J的API-1.5.6.jar,那么你应该也使用SLF4J-简单1.5.6.jar,用SLF4J-简单1.4.2.jar将无法正常工作.

在初始化时,如果SLF4J怀疑可能存在版本不匹配问题,则会发出有关所述不匹配的警告.有关版本不匹配检测机制的确切详细信息,请参阅本FAQ中的相关条目.

这是需要解决的问题.

从命令行使用maven构建良好.但是当我从eclipse启动配置运行项目时,我得到(...)

问题是,你从B SLF4J文物那些从A(及物动词),因此你最终混合的几个版本slf4j-api(1.5.5和1.6.1)和几个绑定(slf4j-log4j12logback-classic).SLF4J在运行时抱怨后来的问题,但你应该解决这两个问题.

从这条消息中我指出我需要将项目A中的绑定升级到1.6.x,但是我没有看到它是如何可能的,因为它包含在hibernate依赖项中.

是的,该消息建议将绑定升级到更新版本.但是,更重要的是,报告有超过类路径上绑定:你需要为已备份的日志记录的log4j和的logback之间进行选择,并提供相应的约束力,但不是他们两个.

是否可以在运行项目B时切换绑定(更新类路径信息),以便它使用1.6.1版本而不是hibernate项目中的版本?

要严格回答关于控制传递依赖关系中的版本的问题,可以使用该dependencyManagement元素来完成.这是一个例子:

<project>
  ...
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.1</version>
      </dependency>
    </dependencies>
  </dependencyManagement>
  ...
  <dependencies>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>3.4.0.GA</version>
    </dependency>
  </dependencies>
  ...
</project>
Run Code Online (Sandbox Code Playgroud)

具有slf4j-api依赖性的工件(如Hibernate EntityManager)将使用版本1.6.1,如下所示:

$ mvn dependency:tree
...
[INFO] +- org.hibernate:hibernate-entitymanager:jar:3.4.0.GA:compile
[INFO] |  +- org.hibernate:ejb3-persistence:jar:1.0.2.GA:compile
[INFO] |  +- org.hibernate:hibernate-commons-annotations:jar:3.1.0.GA:compile
[INFO] |  +- org.hibernate:hibernate-annotations:jar:3.4.0.GA:compile
[INFO] |  +- org.hibernate:hibernate-core:jar:3.3.0.SP1:compile
[INFO] |  |  +- antlr:antlr:jar:2.7.6:compile
[INFO] |  |  \- commons-collections:commons-collections:jar:3.1:compile
[INFO] |  +- org.slf4j:slf4j-api:jar:1.6.1:compile
[INFO] |  +- dom4j:dom4j:jar:1.6.1:compile
[INFO] |  |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO] |  +- javax.transaction:jta:jar:1.1:compile
[INFO] |  \- javassist:javassist:jar:3.4.GA:compile

但正如我所说,真正的问题是你需要在类路径上只有一个绑定.选择log4j或logback,而不是两者,并提供适当的绑定.