找不到 KafkaTemplate bean,即使我已经定义了一个
Can't find KafkaTemplate bean, even though I've defined one
我的环境上下文如下所示:
- Spring Boot v2.5.12
- Maven Multi Module project (this is a module, it imports its Spring Boot dependencies from the parent pom)
- JDK 11
我收到的错误如下所示:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 1 of constructor in com.example.group.service.SomeServiceImpl required a bean of type 'org.springframework.kafka.core.KafkaTemplate' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.kafka.core.KafkaTemplate' in your configuration.
相关文件:
<!-- ${base.dir}/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>
<parent>
<groupId>com.example</groupId>
<artifactId>master-config</artifactId>
<version>1.1.2</version>
</parent>
<groupId>com.example.group</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>app1</module>
<module>app2</module>
</modules>
<properties>
<java.version>11</java.version>
<maven.compiler.release>${java.version}</maven.compiler.release>
<spring.boot.version>2.5.12</spring.boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Omitted -->
</dependencies>
</dependencyManagement>
</project>
<!-- ${base.dir}/app2/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>
<parent>
<groupId>com.example.group</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>app2</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
// ${base.dir}/app2/src/main/java/com/example/group/SomeApplication.java
package com.example.group;
/* Imports omitted */
@SpringBootApplication
public class SomeApplication {
public static void main(String[] args) {
SpringApplication.run(SomeApplication.class, args);
}
}
// ${base.dir}/app2/src/main/java/com/example/group/configuration/AppConfig.java
package com.example.group.configuration;
/* Imports omitted */
@Configuration
public class AppConfig {
@Bean
public KafkaTemplate<Bytes, Object> kafkaTemplate(ProducerFactory<Bytes, Object> kafkaProducerFactory) {
return new KafkaTemplate<>(kafkaProducerFactory);
}
@Bean
public ProducerFactory<Bytes, Object> kafkaProducerFactory() {
Map<String, Object> producerProperties = /* Config omitted */;
return new DefaultKafkaProducerFactory<>(producerProperties);
}
}
// ${base.dir}/app2/src/main/java/com/example/group/service/SomeService.java
package com.example.group.service;
/* Imports omitted */
@Service
public final class SomeServiceImpl implements SomeService {
private static final Logger LOGGER = LoggerFactory.getLogger(SomeServiceImpl.class);
private final KafkaTemplate<Bytes, Object> kafkaTemplate;
private final String topic;
@Autowired
public SomeServiceImpl(
KafkaTemplate<Bytes, Object> kafkaTemplate,
@Value("${topic}") String topic) {
this.kafkaTemplate = kafkaTemplate;
this.topic = topic;
}
/* omitted */
}
这很奇怪,我不明白为什么 Spring 一直告诉我找不到 KafkaTemplate
bean。甚至 IntelliJ 也告诉我它找不到 KafkaTemplate
.
的自动装配候选者
我尝试过的事情
- 正在删除我的自定义
@Bean
s,并依赖 KafkaAutoConfiguration
(它失败了,说找不到 ProducerFactory
)
- 为
KafkaTemplate
bean 命名,并在 SomeService
中使用 @Qualifier
- 移动
@Bean
定义。
None 这些都奏效了,所以我束手无策。任何见解都会有所帮助。
原来这是PEBKAC的情况(键盘和椅子之间存在问题)。
我的配置对象中的 Bytes
类型引用 com.google.common.primitives.Bytes
,我的服务 class 中的 Bytes
引用 org.apache.kafka.common.utils.Bytes
。
自学:
检查你的类型。
我的环境上下文如下所示:
- Spring Boot v2.5.12
- Maven Multi Module project (this is a module, it imports its Spring Boot dependencies from the parent pom)
- JDK 11
我收到的错误如下所示:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 1 of constructor in com.example.group.service.SomeServiceImpl required a bean of type 'org.springframework.kafka.core.KafkaTemplate' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.kafka.core.KafkaTemplate' in your configuration.
相关文件:
<!-- ${base.dir}/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>
<parent>
<groupId>com.example</groupId>
<artifactId>master-config</artifactId>
<version>1.1.2</version>
</parent>
<groupId>com.example.group</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>app1</module>
<module>app2</module>
</modules>
<properties>
<java.version>11</java.version>
<maven.compiler.release>${java.version}</maven.compiler.release>
<spring.boot.version>2.5.12</spring.boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Omitted -->
</dependencies>
</dependencyManagement>
</project>
<!-- ${base.dir}/app2/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>
<parent>
<groupId>com.example.group</groupId>
<artifactId>parent-pom</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>app2</artifactId>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
// ${base.dir}/app2/src/main/java/com/example/group/SomeApplication.java
package com.example.group;
/* Imports omitted */
@SpringBootApplication
public class SomeApplication {
public static void main(String[] args) {
SpringApplication.run(SomeApplication.class, args);
}
}
// ${base.dir}/app2/src/main/java/com/example/group/configuration/AppConfig.java
package com.example.group.configuration;
/* Imports omitted */
@Configuration
public class AppConfig {
@Bean
public KafkaTemplate<Bytes, Object> kafkaTemplate(ProducerFactory<Bytes, Object> kafkaProducerFactory) {
return new KafkaTemplate<>(kafkaProducerFactory);
}
@Bean
public ProducerFactory<Bytes, Object> kafkaProducerFactory() {
Map<String, Object> producerProperties = /* Config omitted */;
return new DefaultKafkaProducerFactory<>(producerProperties);
}
}
// ${base.dir}/app2/src/main/java/com/example/group/service/SomeService.java
package com.example.group.service;
/* Imports omitted */
@Service
public final class SomeServiceImpl implements SomeService {
private static final Logger LOGGER = LoggerFactory.getLogger(SomeServiceImpl.class);
private final KafkaTemplate<Bytes, Object> kafkaTemplate;
private final String topic;
@Autowired
public SomeServiceImpl(
KafkaTemplate<Bytes, Object> kafkaTemplate,
@Value("${topic}") String topic) {
this.kafkaTemplate = kafkaTemplate;
this.topic = topic;
}
/* omitted */
}
这很奇怪,我不明白为什么 Spring 一直告诉我找不到 KafkaTemplate
bean。甚至 IntelliJ 也告诉我它找不到 KafkaTemplate
.
我尝试过的事情
- 正在删除我的自定义
@Bean
s,并依赖KafkaAutoConfiguration
(它失败了,说找不到ProducerFactory
) - 为
KafkaTemplate
bean 命名,并在SomeService
中使用 - 移动
@Bean
定义。
@Qualifier
None 这些都奏效了,所以我束手无策。任何见解都会有所帮助。
原来这是PEBKAC的情况(键盘和椅子之间存在问题)。
我的配置对象中的 Bytes
类型引用 com.google.common.primitives.Bytes
,我的服务 class 中的 Bytes
引用 org.apache.kafka.common.utils.Bytes
。
自学:
检查你的类型。