【问题标题】:Spring kafka error Caused by: java.lang.ClassNotFoundException: org.springframework.kafka.transaction.KafkaAwareTransactionManagerSpring kafka 错误原因:java.lang.ClassNotFoundException: org.springframework.kafka.transaction.KafkaAwareTransactionManager
【发布时间】:2019-05-17 09:31:18
【问题描述】:

这是刚刚启动的带有简单主类的spring boot项目,它在没有spring-kafka依赖的情况下工作正常,但在添加spring-kafkaspring-kafka-test后出现异常,github here

gradle.build

apply plugin: 'java-library'

repositories {
    jcenter()
 }

dependencies {

     compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.0.RELEASE'
     compile group: 'org.springframework.kafka', name: 'spring-kafka', version: '2.0.5.RELEASE'
    testCompile group: 'org.springframework.kafka', name: 'spring-kafka-test', version: '2.0.5.RELEASE'

 }

主类

@SpringBootApplication
public class KafkaMain {

public static void main(String[] args) {
    SpringApplication.run(KafkaMain.class, args);
     }

  }

错误

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-12-16 19:32:29.347 ERROR 39854 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Erro creating beanwithname'org.springframework.boot.autoconfigure.kafka.KafkaAnnotationDrivenConfiguration': Unexpected exception during bean creation; nested exception is java.lang.TypeNotPresentException: Type org.springframework.kafka.transaction.KafkaAwareTransactionManager not present
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:511) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at com.kafka.KafkaMain.main(KafkaMain.java:10) [bin/:na]
Caused by: java.lang.TypeNotPresentException: Type org.springframework.kafka.transaction.KafkaAwareTransactionManager not present

【问题讨论】:

    标签: java spring-boot spring-kafka spring-kafka-test


    【解决方案1】:

    Spring for Apache Kafka 2.0.x 与 Spring Boot 2.1.x 不兼容。您必须使用Spring-Kafka 2.2.x。更多地依赖于 Spring Boot 本身的依赖关系会更好。请参阅https://start.spring.io/ 了解如何正确启动 Spring Boot 项目的更多信息。

    如果没有被问到,请不要在不同的地方重复您的问题。

    【讨论】:

    • 嗯?这怎么可能?实际上,在现实中,Spring Boot 规定了所有版本的依赖项。所有的 Spring Boot 项目都是从上面提到的生成工具开始的。我建议忘记使用 Spring Boot 的 Spring Kafka 版本。否则你的用例不清楚
    • 因为你升级了Spring Boot,但没有升级Spring Kafka。你需要学习Spring Boot是如何管理依赖的,所以你只需要升级它的版本,不用担心其他版本
    • 工作! - 我搬到了 2.2.4.RELEASE org.springframework.kafkaspring-kafka2.2.4.RELEASE它正在工作!谢谢。
    • 非常感谢@Artem,它对我有用,在降级我的春季版本后,它按预期工作。
    • 我已经从 2.1.3 更新到 2.2.4,它的工作。非常感谢。
    猜你喜欢
    • 2021-03-03
    • 2019-10-23
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 2015-08-15
    相关资源
    最近更新 更多