【问题标题】:Failed to instantiate [org.springframework.cloud.stream.binding.BindingService]: Factory method 'bindingService' threw exception无法实例化 [org.springframework.cloud.stream.binding.BindingService]:工厂方法 'bindingService' 抛出异常
【发布时间】:2019-11-07 03:21:48
【问题描述】:

我正在尝试使用 Spring Cloud Stream 和 Apache Kafka 作为流绑定器来开发事件驱动架构。

Spring Boot 版本:2.1.9 发布 Spring Cloud 版本:2.2.1 发布

我尝试了与 Greenwich.SR3 云版本相同的配置,但同样的错误。

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.9.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.adi</groupId>
    <artifactId>ProducerKafka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ProducerKafka</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud-stream.version>2.2.1.RELEASE</spring-cloud-stream.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream</artifactId>
            <version>${spring-cloud-stream.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-kafka-streams</artifactId>
            <version>${spring-cloud-stream.version}</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
<!--        <dependency> -->
<!--            <groupId>org.springframework.cloud</groupId> -->
<!--            <artifactId>spring-cloud-stream-test-support</artifactId> -->
<!--            <scope>test</scope> -->
<!--        </dependency> -->
    </dependencies>



    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

MainApplication.java


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;

@SpringBootApplication
@EnableBinding(Source.class)
public class ProducerKafkaApplication {

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

}

在将应用程序作为 Spring Boot 应用程序运行时,在控制台出现以下错误。

2019-11-07 08:37:58.780  INFO 513 --- [           main] com.adi.ProducerKafkaApplication         : No active profile set, falling back to default profiles: default
2019-11-07 08:38:01.121  INFO 513 --- [           main] o.s.c.a.ConfigurationClassEnhancer       : @Bean method BinderFactoryConfiguration.implicitFunctionBinder is non-static and returns an object assignable to Spring's BeanFactoryPostProcessor interface. This will result in a failure to process annotations such as @Autowired, @Resource and @PostConstruct within the method's declaring @Configuration class. Add the 'static' modifier to this method to avoid these container lifecycle issues; see @Bean javadoc for complete details.
2019-11-07 08:38:01.193  INFO 513 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.
2019-11-07 08:38:01.216  INFO 513 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.
2019-11-07 08:38:01.224  INFO 513 --- [           main] faultConfiguringBeanFactoryPostProcessor : No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
2019-11-07 08:38:01.270  INFO 513 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.kafka.annotation.KafkaBootstrapConfiguration' of type [org.springframework.kafka.annotation.KafkaBootstrapConfiguration$$EnhancerBySpringCGLIB$$a9058eff] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-07 08:38:01.318  INFO 513 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'integrationDisposableAutoCreatedBeans' of type [org.springframework.integration.config.annotation.Disposables] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-07 08:38:01.338  INFO 513 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.integration.config.IntegrationManagementConfiguration' of type [org.springframework.integration.config.IntegrationManagementConfiguration$$EnhancerBySpringCGLIB$$404538ab] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-07 08:38:01.352  INFO 513 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration$IntegrationJmxConfiguration' of type [org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration$IntegrationJmxConfiguration$$EnhancerBySpringCGLIB$$1bd743cb] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-07 08:38:01.365  INFO 513 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration' of type [org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration$$EnhancerBySpringCGLIB$$54beec98] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-07 08:38:01.370  INFO 513 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'mbeanServer' of type [com.sun.jmx.mbeanserver.JmxMBeanServer] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-11-07 08:38:01.970  INFO 513 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-11-07 08:38:02.020  INFO 513 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-11-07 08:38:02.021  INFO 513 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.26]
2019-11-07 08:38:02.264  INFO 513 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-11-07 08:38:02.265  INFO 513 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3328 ms
2019-11-07 08:38:03.122  INFO 513 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-11-07 08:38:03.808  INFO 513 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2019-11-07 08:38:03.815  WARN 513 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bindingService' defined in class path resource [org/springframework/cloud/stream/config/BindingServiceConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.stream.binding.BindingService]: Factory method 'bindingService' threw exception; nested exception is java.lang.NoClassDefFoundError: javax/validation/Validator
2019-11-07 08:38:03.815  INFO 513 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Shutting down ExecutorService 'taskScheduler'
2019-11-07 08:38:03.818  INFO 513 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2019-11-07 08:38:03.821  INFO 513 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-11-07 08:38:03.835  INFO 513 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-11-07 08:38:03.902 ERROR 513 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bindingService' defined in class path resource [org/springframework/cloud/stream/config/BindingServiceConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.stream.binding.BindingService]: Factory method 'bindingService' threw exception; nested exception is java.lang.NoClassDefFoundError: javax/validation/Validator
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:607) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1321) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1160) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:847) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:744) [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:391) [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1204) [spring-boot-2.1.9.RELEASE.jar:2.1.9.RELEASE]
    at com.adi.ProducerKafkaApplication.main(ProducerKafkaApplication.java:15) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.stream.binding.BindingService]: Factory method 'bindingService' threw exception; nested exception is java.lang.NoClassDefFoundError: javax/validation/Validator
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    ... 19 common frames omitted
Caused by: java.lang.NoClassDefFoundError: javax/validation/Validator
    at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_161]
    at java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~[na:1.8.0_161]
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[na:1.8.0_161]
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) ~[na:1.8.0_161]
    at java.net.URLClassLoader.access$100(URLClassLoader.java:73) ~[na:1.8.0_161]
    at java.net.URLClassLoader$1.run(URLClassLoader.java:368) ~[na:1.8.0_161]
    at java.net.URLClassLoader$1.run(URLClassLoader.java:362) ~[na:1.8.0_161]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_161]
    at java.net.URLClassLoader.findClass(URLClassLoader.java:361) ~[na:1.8.0_161]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_161]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) ~[na:1.8.0_161]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_161]
    at org.springframework.cloud.stream.binding.BindingService.<init>(BindingService.java:86) ~[spring-cloud-stream-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.cloud.stream.config.BindingServiceConfiguration.bindingService(BindingServiceConfiguration.java:204) ~[spring-cloud-stream-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.cloud.stream.config.BindingServiceConfiguration$$EnhancerBySpringCGLIB$$fb550fc9.CGLIB$bindingService$1(<generated>) ~[spring-cloud-stream-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.cloud.stream.config.BindingServiceConfiguration$$EnhancerBySpringCGLIB$$fb550fc9$$FastClassBySpringCGLIB$$56848299.invoke(<generated>) ~[spring-cloud-stream-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    at org.springframework.cloud.stream.config.BindingServiceConfiguration$$EnhancerBySpringCGLIB$$fb550fc9.bindingService(<generated>) ~[spring-cloud-stream-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_161]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_161]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_161]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE]
    ... 20 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.validation.Validator
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_161]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_161]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) ~[na:1.8.0_161]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_161]
    ... 44 common frames omitted

谢谢, 阿迪亚

【问题讨论】:

    标签: spring-boot apache-kafka-streams spring-cloud-stream


    【解决方案1】:

    您似乎没有为 Validator 类添加依赖项。在您的pom.xml 文件中添加以下内容。

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.0.Final</version>
    </dependency>
    

    编辑

    要添加所有与验证相关的依赖项,请添加以下内容:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
        <version>${spring-cloud-stream.version}</version>
    </dependency>
    

    【讨论】:

    • 否,添加依赖项并不能解决问题。现在出现以下错误 org.springframework.beans.factory.UnsatisfiedDependencyException:创建 org.springframework.cloud.stream.config.BinderFactoryConfiguration 中定义的名称为 'integrationMessageHandlerMethodFactory' 的 bean 时出错:通过方法 'messageHandlerMethodFactory' 参数 1 表达的依赖关系不满足;
    • 您能否将您的 artifactId 从 spring-cloud-stream-binder-kafka-streams 更新为 spring-cloud-stream-binder-kafka 并重试?如果您收到错误,请提供来自堆栈跟踪的caused by
    • 修改artifactid后,还是报同样的错误。在堆栈跟踪中,显示以下错误原因:java.lang.NoClassDefFoundError:无法初始化类 org.hibernate.validator.internal.engine.ConfigurationImpl
    • 在 pom.xml 中添加 hibernate-validator 依赖项后,我仍然收到相同的错误:java.lang.NoClassDefFoundError: Could not initialize class org.hibernate.validator.internal.engine.ConfigurationImpl只有一个问题,我正在使用 Spring Boot。 Spring boot 不应该像管理其他人一样管理所有所需的依赖项吗?对此有任何想法。
    • @adityasingh,是的,你在某种程度上是正确的。 Spring boot 管理几乎所有的依赖项。但只有那些包含在spring-boot-starter-web 或其他此类弹簧靴的工件中的。因此,通过在 pom 中定义spring-boot-starter-web,它将自动下载所有在spring-boot-starter-web 中定义的依赖项。其他需要我们手动提供的依赖。
    【解决方案2】:

    升级到 spring-cloud-starter-stream-rabbit 的最新依赖项。

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 2021-11-07
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 2020-02-15
      • 1970-01-01
      相关资源
      最近更新 更多