英文:
Why @autowired is not required?
问题
为什么对于后续的方法,我不需要要求使用 @Autowired 来注入 consumerFactory?而当我这样做时,会收到循环依赖的异常。
我是否漏掉了一些基本的东西,我猜是的。
感谢帮助。
@Bean
public ConsumerFactory<String, String> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(consumerConfig());
}
@Bean
public KafkaListenerContainerFactory<
ConcurrentMessageListenerContainer<String, String>> factory(
ConsumerFactory<String, String> consumerFactory
) {
ConcurrentKafkaListenerContainerFactory<String, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory);
return factory;
}
英文:
why for a later method I do not need to require @Autowired to inject consumerFactory? And when I do so I receive a circular dependency exception.
Am I missing something fundamental, I guess yes.
Thanks for the help.
@Bean
public ConsumerFactory<String, String> consumerFactory() {
return new DefaultKafkaConsumerFactory<>(consumerConfig());
}
@Bean
public KafkaListenerContainerFactory<
ConcurrentMessageListenerContainer<String, String>> factory(
ConsumerFactory<String, String> consumerFactory
) {
ConcurrentKafkaListenerContainerFactory<String, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory);
return factory;
}
答案1
得分: 1
Spring会自动检测带有@Bean
注解的factory()
方法需要一个ConsumerFactory
来创建KafkaListenerContainerFactory
的实例。因此,它已经知道需要为此提供一个依赖项。
只有在你的类需要一个依赖项时才需要使用@Autowired
,例如:
@Component
public class SomeClass {
@Autowired
private ConsumerFactory<String, String> consumerFactory;
}
另一种方法是将其定义为构造函数参数,然后不需要@Autowired
,因为构造函数签名已经清楚:
@Component
public class SomeClass {
private ConsumerFactory<String, String> consumerFactory;
public SomeClass(ConsumerFactory<String, String> consumerFactory) {
this.consumerFactory = consumerFactory;
}
}
英文:
Spring automatically detects that the @Bean
annotated method factory()
requires a ConsumerFactory
to create an instance of KafkaListenerContainerFactory
. Hence it already knows that it needs to provide a dependency for that.
@Autowired
is only required if you have a class that requires a dependency, for example:
@Component
public class SomeClass {
@Autowired
private ConsumerFactory<String, String> consumerFactory;
}
An alternative to this would be to define this as constructor argument, then you don't need @Autowired
as well as it's already clear thanks to the constructor signature:
@Component
public class SomeClass {
private ConsumerFactory<String, String> consumerFactory;
public SomeClass(ConsumerFactory<String, String> consumerFactory) {
this.consumerFactory = consumerFactory;
}
}
答案2
得分: 1
你在同一类中使用了一个方法来初始化ConsumerFactory对象(我怀疑它是一个带有**@Configuration**注解的配置类)。
@Bean注解创建了另一个元素并将其添加到Spring引擎维护的集合中。
英文:
You use a method defined in the same class to initialize the ConsumerFactory object (I suspect it's a configuration class annotated with @Configuration).
The @Bean annotation creates another element and adds it to the set maintained by Spring engine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论