Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Why does this work:

@Configuration
public class MyConfig {

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

  @Bean               // <-- Shouldn't I need @Autowired here?
  public B getB(A a) {
    return new B(a);
  }  
}

Thanks!

like image 327
Jan Żankowski Avatar asked Aug 18 '15 17:08

Jan Żankowski


People also ask

Can we Autowire a bean in configuration class?

In addition to being able to reference any particular bean definition as seen above, one @Configuration class may reference the instance of any other @Configuration class using @Autowired . This works because the @Configuration classes themselves are instantiated and managed as individual Spring beans.

How do you Autowire a bean method?

Enabling @Autowired annotationBy declaring beans, you provide metadata to the Spring Container to return the required dependency object at runtime. This is called Spring Bean Autowiring. In java based configuration, all the bean methods are defined in the class with @configuration annotation.

Can we use @bean without @configuration?

@Bean methods may also be declared within classes that are not annotated with @Configuration. For example, bean methods may be declared in a @Component class or even in a plain old class. In such cases, a @Bean method will get processed in a so-called 'lite' mode.

Can we use @bean on class?

We can also declare beans using the @Bean annotation in a configuration class. Finally, we can mark the class with one of the annotations from the org. springframework.


2 Answers

@Autowire lets you inject beans from context to "outside world" where outside world is your application. Since with @Configuration classes you are within "context world ", there is no need to explicitly autowire (lookup bean from context).

Think of analogy like when accessing method from a given instance. While you are within the instance scope there is no need to write this to access instance method, but outside world would have to use instance reference.

Edit

When you write @Configuration class, you are specifying meta data for beans which will be created by IOC.

@Autowire annotation on the other hand lets you inject initialized beans, not meta-data, in application. So, there is no need for explicit injection because you are not working with Beans when inside Configuration class.

like image 92
John Avatar answered Oct 03 '22 16:10

John


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).

enter image description here

like image 42
mzoz Avatar answered Oct 03 '22 18:10

mzoz