为什么不需要 @autowired?

huangapple go评论66阅读模式
英文:

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&lt;String, String&gt; consumerFactory() {
        return new DefaultKafkaConsumerFactory&lt;&gt;(consumerConfig());
    }

    @Bean
    public KafkaListenerContainerFactory&lt;
            ConcurrentMessageListenerContainer&lt;String, String&gt;&gt; factory(
            ConsumerFactory&lt;String, String&gt; consumerFactory
    ) {

        ConcurrentKafkaListenerContainerFactory&lt;String, String&gt; factory =
                new ConcurrentKafkaListenerContainerFactory&lt;&gt;();

        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&lt;String, String&gt; 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&lt;String, String&gt; consumerFactory;

   public SomeClass(ConsumerFactory&lt;String, String&gt; 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.

huangapple
  • 本文由 发表于 2023年7月12日 22:28:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671673.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定