spring-boot-starter-amqp 依赖导致 ***Error
Posted
技术标签:
【中文标题】spring-boot-starter-amqp 依赖导致 ***Error【英文标题】:spring-boot-starter-amqp dependency causes ***Error 【发布时间】:2017-05-12 21:34:02 【问题描述】:我有一个基本的 Spring Boot 项目,我尝试使用 RabbitMQ 实现简单的消息传递。当我将 spring-boot-starter-amqp 依赖项添加到我的 pom.xml
文件并启动 Spring Boot 应用程序时,我得到由 java.lang.***Error
引起的 java.lang.reflect.InvocationTargetException
异常:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:58)
Caused by: java.lang.***Error
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:280)
at org.apache.log4j.Category.<init>(Category.java:57)
at org.apache.log4j.Logger.<init>(Logger.java:37)
at org.apache.log4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:43)
at org.apache.log4j.LogManager.getLogger(LogManager.java:45)
at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:63)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
at org.apache.log4j.Category.<init>(Category.java:57)
at org.apache.log4j.Logger.<init>(Logger.java:37)
at org.apache.log4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:43)
at org.apache.log4j.LogManager.getLogger(LogManager.java:45)
at org.slf4j.impl.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:63)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
at org.apache.log4j.Category.<init>(Category.java:57)
at org.apache.log4j.Logger.<init>(Logger.java:37)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>rabbitmq.client</groupId>
<artifactId>rabbitmqFstClient</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>rabbitmqFstClient</name>
<description>Spring-Boot rabbitmq client</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-jms</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.14.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
如果我从 pom.xml 中删除 spring-boot-starter-amqp 依赖项,则项目可以正常运行:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
【问题讨论】:
【参考方案1】:我不确定它是否能解决您的问题,但您仍然存在潜在的冲突,因为根据日志您拉取了 log4J 依赖项。 如果这样做,则必须排除声明时默认提供的 logback:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
您可以通过显示项目的依赖树并查看 org.springframework.boot:spring-boot-starter-web:jar
依赖拉取的内容来检查它:
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.4.0.RELEASE:compile
[INFO] | +- org.springframework.boot:spring-boot-starter:jar:1.4.0.RELEASE:compile
[INFO] | | +- org.springframework.boot:spring-boot-starter-logging:jar:1.4.0.RELEASE:compile
[INFO] | | | +- ch.qos.logback:logback-classic:jar:1.1.7:compile
[INFO] | | | | +- ch.qos.logback:logback-core:jar:1.1.7:compile
[INFO] | | | | \- org.slf4j:slf4j-api:jar:1.7.21:compile
[INFO] | | | +- org.slf4j:jcl-over-slf4j:jar:1.7.21:compile
[INFO] | | | +- org.slf4j:jul-to-slf4j:jar:1.7.21:compile
[INFO] | | | \- org.slf4j:log4j-over-slf4j:jar:1.7.21:compile
[INFO] | | \- org.yaml:snakeyaml:jar:1.17:runtime
Spring Boot 声明如果它在类路径上,它支持 Log4j 2 用于日志记录配置。如果您使用启动器来组装依赖项,则意味着您必须排除 Logback,然后再包含 log4j 2。
所以要仅使用 log4j,您可以从 spring-boot-starter-web 依赖项中排除 Logback:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
或者仅使用 logback,您应该排除可能由您声明的其他依赖项之一拉取的 log4j。
使用:来自 Maven 项目的mvn dependency:tree
来了解罪魁祸首。
但无论如何,你不应该同时使用两者。
【讨论】:
【参考方案2】:如果您打算通过 RabbitMQ 使用 AMQP,则删除所有 JMS 依赖项。
另外,对于一个简单的应用程序,您也不需要 Spring Integration。仅使用 spring-boot-starter-amqp
尝试您的应用程序【讨论】:
以上是关于spring-boot-starter-amqp 依赖导致 ***Error的主要内容,如果未能解决你的问题,请参考以下文章