- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 Kafka 的 Spring 配置:
import org.apache.kafka.clients.producer.ProducerConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import java.util.HashMap;
import java.util.Map;
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "kafka.status.producer")
public class StatusKafkaProducerConfig {
private String keySerializer;
private String valueSerializer;
private String bootstrapServers;
public void setKeySerializer(String keySerializer) {
this.keySerializer = keySerializer;
}
public void setValueSerializer(String valueSerializer) {
this.valueSerializer = valueSerializer;
}
public void setBootstrapServers(String bootstrapServers) {
this.bootstrapServers = bootstrapServers;
}
@Bean
public Map<String, Object> producerConfigs() {
Map<String, Object> properties = new HashMap<>();
properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, keySerializer);
properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, valueSerializer);
properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
return properties;
}
@Bean
@RefreshScope
public KafkaTemplate<String, String> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
@Bean
public ProducerFactory<String, String> producerFactory() {
return new DefaultKafkaProducerFactory<>(producerConfigs());
}
}
我将项目迁移到最新的 Spring cloud 2021.0.1 和 Spring Boot 2.6.6。但是我在启动时收到以下错误堆栈:
2022-04-30 19:14:08.350 ERROR 23684 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'producerFactory' defined in class path resource [com/test/StatusUpdateKafkaProducerConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.kafka.core.ProducerFactory]: Factory method 'producerFactory' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.19.jar:5.3.19]
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486) ~[spring-beans-5.3.19.jar:5.3.19]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-5.3.19.jar:5.3.19]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) ~[spring-beans-5.3.19.jar:5.3.19]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.19.jar:5.3.19]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.19.jar:5.3.19]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.19.jar:5.3.19]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.19.jar:5.3.19]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.19.jar:5.3.19]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.19.jar:5.3.19]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.19.jar:5.3.19]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.7.jar:2.6.7]
at com.test.Application.main(Application.java:82) ~[main/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.kafka.core.ProducerFactory]: Factory method 'producerFactory' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.19.jar:5.3.19]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.19.jar:5.3.19]
... 19 common frames omitted
Caused by: java.lang.NullPointerException: null
at java.base/java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011) ~[na:na]
at java.base/java.util.concurrent.ConcurrentHashMap.putAll(ConcurrentHashMap.java:1089) ~[na:na]
at java.base/java.util.concurrent.ConcurrentHashMap.<init>(ConcurrentHashMap.java:852) ~[na:na]
at org.springframework.kafka.core.DefaultKafkaProducerFactory.<init>(DefaultKafkaProducerFactory.java:205) ~[spring-kafka-2.8.5.jar:2.8.5]
at org.springframework.kafka.core.DefaultKafkaProducerFactory.<init>(DefaultKafkaProducerFactory.java:168) ~[spring-kafka-2.8.5.jar:2.8.5]
at com.test.StatusUpdateKafkaProducerConfig.producerFactory(TransactionStatusUpdateKafkaProducerConfig.java:53) ~[commons-0.0.1-plain.jar:na]
at com.test.StatusUpdateKafkaProducerConfig$$EnhancerBySpringCGLIB$$e907de88.CGLIB$producerFactory$0(<generated>) ~[commons-0.0.1-plain.jar:na]
at com.test.StatusUpdateKafkaProducerConfig$$EnhancerBySpringCGLIB$$e907de88$$FastClassBySpringCGLIB$$db10662a.invoke(<generated>) ~[commons-0.0.1-plain.jar:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.19.jar:5.3.19]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.19.jar:5.3.19]
at com.StatusUpdateKafkaProducerConfig$$EnhancerBySpringCGLIB$$e907de88.producerFactory(<generated>) ~[commons-0.0.1-plain.jar:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:282) ~[spring-core-5.3.19.jar:5.3.19]
at org.springframework.cloud.context.scope.GenericScope$LockedScopedProxyFactoryBean.invoke(GenericScope.java:485) ~[spring-cloud-context-3.1.2.jar:3.1.2]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.3.19.jar:5.3.19]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763) ~[spring-aop-5.3.19.jar:5.3.19]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708) ~[spring-aop-5.3.19.jar:5.3.19]
at com.StatusUpdateKafkaProducerConfig$$EnhancerBySpringCGLIB$$6800bd04.producerFactory(<generated>) ~[commons-0.0.1-plain.jar:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.19.jar:5.3.19]
... 20 common frames omitted
你知道我该如何解决这个问题吗?
最佳答案
错误堆栈跟踪表明在 producerFactory()
方法中创建 DefaultKafkaProducerFactory
的新实例时发生了 NullPointerException
.
在内部,DefaultKafkaProducerFactory
creates a ConcurrentHashMap
使用提供的信息来维护其配置。
正如您在 ConcurrentHashMap
javadocs 中看到的那样,如果提供的任何键或值是 null
,put
方法将引发 NullPointerException
。
因此,keySerializer
、valueSerializer
或 bootstrapServers
中的一个或多个变量可能为空。
我假设这些值是根据您的 @ConfigurationProperties
kafka.status.producer
通过依赖注入(inject)填充的。请确保在您的 Spring 配置中正确定义了这些属性。
出于测试目的,请尝试删除 @RefreshScope
注释:它可能是问题的原因。
反之亦然。如 Spring Cloud 文档中所示 talking about @RefreshScope
:
@RefreshScope
works (technically) on a@Configuration
class,but it might lead to surprising behavior. For example, it does notmean that all the@Beans
defined in that class are themselves in@RefreshScope
. Specifically, anything that depends on those beanscannot rely on them being updated when a refresh is initiated, unlessit is itself in@RefreshScope
. In that case, it is rebuilt on arefresh and its dependencies are re-injected. At that point, they arere-initialized from the refreshed@Configuration
).
从这个角度来看,如果用 @RefreshScope
注释 producerFactory
bean 也是完全合理的。我会尝试:
import org.apache.kafka.clients.producer.ProducerConfig;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import java.util.HashMap;
import java.util.Map;
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "kafka.status.producer")
public class StatusKafkaProducerConfig {
private String keySerializer;
private String valueSerializer;
private String bootstrapServers;
public void setKeySerializer(String keySerializer) {
this.keySerializer = keySerializer;
}
public void setValueSerializer(String valueSerializer) {
this.valueSerializer = valueSerializer;
}
public void setBootstrapServers(String bootstrapServers) {
this.bootstrapServers = bootstrapServers;
}
@Bean
@RefreshScope
public Map<String, Object> producerConfigs() {
Map<String, Object> properties = new HashMap<>();
properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, keySerializer);
properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, valueSerializer);
properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
return properties;
}
@Bean
@RefreshScope
public KafkaTemplate<String, String> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
@Bean
@RefreshScope
public ProducerFactory<String, String> producerFactory() {
return new DefaultKafkaProducerFactory<>(producerConfigs());
}
}
请注意包含额外的 @RefreshScope
注释。
最后,考虑查看 Spring Kafka 项目文档,它提供了几个有关如何配置项目的示例,无论您是否使用 Spring Boot或 not ,并为您提供有关 DefaultKafkaProducerFactory
的更多背景信息.
正如您在上述文档中所见,如果您使用的是 Spring Boot,就像您的情况一样,代码可以大大简化。考虑查看 Spring Boot
related documentation关于主题和related properties configuration .
关于java - 通过工厂方法实例化 Bean 失败;异常 org.springframework.beans.BeanInstantiationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69067277/
刚开始处理Maven和Spring。当我尝试创建DAO和ResultSet然后运行应用程序时,抛出errors: Error:(4, 31) java: package org.springframe
问题: Error:(15, 10) java: cannot find symbol symbol: class SpringRunner Error:(16, 2) java: cannot
我正在尝试构建这个 RESTful 服务示例:https://spring.io/guides/gs/rest-service/GreetingController.java 的导入没有错误: pac
如果我尝试向构造函数注入(inject) Facebook 参数,我将尝试使用 facebook api 使用 spring + thymeleaf + hibernate 创建 Facebook 应
如何解决Spring中Bean的自动连接歧义?我们有一个 Dessert 接口(interface),并且有实现该接口(interface)(Dessert)的三种不同的甜点(Bean)。 今天的甜点
请问为什么我的 pom.xml 文件中会出现此错误 Missing artifact org.springframework:spring-context:jar:${org.springframew
让 gradle 构建正常工作( from a previous question related to this one ),安迪·威尔金森(Andy Wilkinson)为我回答,没有问题。正在为
我正在 tomcat 7 中开发网站(spring 3.1.1),但出现错误 ERROR: org.springframework.web.context.ContextLoader - Contex
我在将航类信息保存到 mysql 数据库时遇到错误。请帮助我下面是我的代码: 我已经尝试了所有方法,添加模式和这么多 它不会从字符串转换为日期 控制台日志 2020-05-12 13:19:21.04
我使用intellij创建了一个小型java应用程序,后来我使用“添加框架支持”选项将该项目更新为Maven项目。当我厌倦了在项目上添加 spring jar 文件时,出现以下错误:“没有为 org.
我尝试使用 org.springframework.data.mongodb.core.MongoOperations 从 mongo 集合中查询记录。我在 CompanyTemplRepoImpl
我尝试将 Spring4 与 Hibernate5 一起使用,但出现此错误: org.springframework.orm.jpa.EntityManagerHolder cannot be cas
我是这个论坛的新手。我正在尝试使用 spring 3.2.6 和 tomcat 7.0 制作一个应用程序。我已将所有必需的 jar 添加到 WEB-INF/lib 文件夹中。 DispatcherSe
我在 maven 架构中使用 Spring 框架 4.0.1.RELEASE、OAuth Security 2.0.7.RELEASE,当我编译代码时,出现以下错误。 SEVERE: Exceptio
我正在使用以下指令开发 CRUD Web 应用程序: https://www.javaguides.net/2019/02/spring-boot-2-angular-7-crud-example-t
这个问题已经有答案了: what is the difference in org.springframework.web.servlet.ModelAndView vs org.springfram
我正在尝试将 hibernate 与 spring boot 一起使用。但我收到此错误:org.springframework.orm.jpa.EntityManagerHolder 无法转换为 or
我尝试创建简单的用户登录和注册页面。但我无法使用服务方法创建用户。我有创建新用户的服务。 @Service public class LocalUserDetailsService implement
我用 STS、Roo 和 GWT 创建了一个新项目,并尝试包含 Spring Security。 从那时起,我收到以下错误。有没有人知道出了什么问题?! org.springframework.bea
这些是我正在使用的版本和依赖项 我怀疑版本需要更改但更改为什么,我不确定 springCore : '5.3.3', springjdbc
我是一名优秀的程序员,十分优秀!