【问题标题】:Error init SpringBoot app. I have a SpringBoot app. 2.1.5.RELEASE that uses Kakfa初始化 SpringBoot 应用程序时出错。我有一个 SpringBoot 应用程序。 2.1.5.RELEASE 使用 Kafka
【发布时间】:2019-11-07 20:57:54
【问题描述】:

我有一个 SpringBoot 应用程序。 2.1.5.RELEASE 使用 Spring for Apache Kafka(基于 Kafka 的消息传递解决方案),带有 IntelliJ IDEA 我创建了这个服务:

public class KafkaProducerService {

    private static final String TOPIC = "bendiciones";

    private final KafkaTemplate<String,String> kafkaTemplate;

    public KafkaProducerService(KafkaTemplate<String, String> kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

    public void sendMessage(String message){
        log.info(String.format("$$ -> Producing message --> %s",message));
        this.kafkaTemplate.send(TOPIC,message);
    }
}

但是当我启动应用程序时。我收到了这个错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.kafka.config.internalKafkaListenerAnnotationProcessor': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.kafka.annotation.KafkaListenerAnnotationBeanPostProcessor]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/core/log/LogAccessor
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1303)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1197)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:228)
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:721)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:534)
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    at com.bendiciones.buenas.noches.Application.main(Application.java:38)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.kafka.annotation.KafkaListenerAnnotationBeanPostProcessor]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/core/log/LogAccessor
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:184)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1295)
    ... 15 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/springframework/core/log/LogAccessor

这些是我在 po, 文件中的依赖项:

       <dependency>
                    <groupId>org.springframework.kafka</groupId>
                    <artifactId>spring-kafka</artifactId>
                    <version>2.3.3.RELEASE</version>
                </dependency>

    <dependency>
                <groupId>com.fasterxml.jackson.datatype</groupId>
                <artifactId>jackson-datatype-jsr310</artifactId>
            </dependency>
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>

【问题讨论】:

  • 这样的错误来自混合不同版本的spring的jar,即不兼容。确保您使用 spring-boot-starters 来获取正确的依赖项。
  • 您正在使用 Spring Boot 2.1.x,它使用 Spring 5.1,您正在使用 Spring Kafka 2.3.x,它需要 Spring 5.2。不兼容的版本。改用 2.2.x 或升级到 Spring Boot 2.2.x。
  • @M.Deinum,请转换为答案

标签: spring spring-boot spring-mvc apache-kafka


【解决方案1】:

这类错误通常来自于混合来自不同版本框架的 jar。它们都应该匹配,否则你会遇到这类问题。

查看您的依赖关系,您使用的是 Spring Boot 2.1.x,它依赖于 Spring Framework 5.1.x。您还想使用 Kafka,其中包括 Spring Kafka 2.3.3,但是此版本使用/预期 Spring 5.2。 Spring 5.1 和 5.2 不兼容,因此您遇到了问题。

从 Spring Kafka 依赖项中删除 &lt;version&gt; 标签。 Spring Boot 会自动为您管理对适当兼容版本的依赖关系。

【讨论】:

    猜你喜欢
    • 2020-01-30
    • 2020-01-20
    • 2018-01-13
    • 1970-01-01
    • 2023-04-10
    • 2017-12-03
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多