Why do I not need @Autowired on @Bean methods in a Spring configuration class?

前端 未结 3 1666
忘掉有多难
忘掉有多难 2020-12-24 15:22

Why does this work:

@Configuration
public class MyConfig {

  @Bean
  public A getA() {
    return new A();
  }

  @Bean               // <-- Shouldn\'t I          


        
3条回答
  •  无人及你
    2020-12-24 15:49

    Hi Jan your question is marked as answered over 4 years ago but I've found a better source: https://www.logicbig.com/tutorials/spring-framework/spring-core/javaconfig-methods-inter-dependency.html

    here's another article with the same idea: https://dzone.com/articles/spring-configuration-and, it also states that such usage is not well documented which I found true. (?)

    so basically if beanA's initialization depends on beanB, spring will wire them without explicit @Autowired annotation as long as you declare these two beans in the application context (i.e. @Configuartion class).

提交回复
热议问题