Spring Kafka bean 配置 - 为什么调用 bean 方法而不是自动装配?

Spring Kafka bean configuration- why invoking bean method instead of autowire?

我在有关 spring kafka bean 声明的教程和 github 项目中看到了这种模式,并且 我不明白为什么直接调用bean方法而不是自动装配,

例如,
https://www.baeldung.com/spring-kafka 第 4 节中:

@Configuration 
public class KafkaProducerConfig {

@Bean
public ProducerFactory<String, String> producerFactory() {
    Map<String, Object> configProps = new HashMap<>();
    configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    return new DefaultKafkaProducerFactory<>(configProps);
}

@Bean
public KafkaTemplate<String, String> kafkaTemplate() {
    return new KafkaTemplate<>(producerFactory());
}
}

为什么要调用 producerFactory 方法?

这样声明不是更好吗?

@Configuration 
public class KafkaProducerConfig {

@Bean
public ProducerFactory<String, String> producerFactory() {
    Map<String, Object> configProps = new HashMap<>();
    configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapAddress);
    configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
    return new DefaultKafkaProducerFactory<>(configProps);
}

@Bean
public KafkaTemplate<String, String> kafkaTemplate(ProducerFactory<String, String> producerFactory) {
    return new KafkaTemplate<>(producerFactory);
}
}

似乎创建了两个 DefaultKafkaProducerFactory 实例,而不是一个。 我错过了什么?

不,没有创建该对象的两个实例,因为 producerFactory()BeanFactory 代理,正是这种情况。

虽然我同意现代方法注入变体更好,尤其是当我们追求启动时的性能提升时。

参见 @Configuration.proxyBeanMethods JavaDocs:

/**
 * Specify whether {@code @Bean} methods should get proxied in order to enforce
 * bean lifecycle behavior, e.g. to return shared singleton bean instances even
 * in case of direct {@code @Bean} method calls in user code. This feature
 * requires method interception, implemented through a runtime-generated CGLIB
 * subclass which comes with limitations such as the configuration class and
 * its methods not being allowed to declare {@code final}.
 * <p>The default is {@code true}, allowing for 'inter-bean references' via direct
 * method calls within the configuration class as well as for external calls to
 * this configuration's {@code @Bean} methods, e.g. from another configuration class.
 * If this is not needed since each of this particular configuration's {@code @Bean}
 * methods is self-contained and designed as a plain factory method for container use,
 * switch this flag to {@code false} in order to avoid CGLIB subclass processing.
 * <p>Turning off bean method interception effectively processes {@code @Bean}
 * methods individually like when declared on non-{@code @Configuration} classes,
 * a.k.a. "@Bean Lite Mode" (see {@link Bean @Bean's javadoc}). It is therefore
 * behaviorally equivalent to removing the {@code @Configuration} stereotype.
 * @since 5.2
 */
boolean proxyBeanMethods() default true;