Close

Spring - Beans Auto-Wiring

[Last Updated: May 15, 2023]

Spring container can autowire dependencies implicitly.

We can also specify a mode of autowiring using @Bean annotation attribute 'autowire'

@Configuration
 public class Config{
    @Bean(autowire == <autowireMode>)
     public ABean aBean(){
        return new ABean();
    }
   .....
 }

Note: The autowire attribute has been deprecated in Spring 5.1 and removed in Spring 6. Per Spring Spring 5.1 doc:

Deprecated. as of 5.1, since @Bean factory method argument resolution and @Autowired processing supersede name/type-based bean property injection


If you are using older version (pre 5.1) then you may need to understand how to use autowire attribute.

The valid values of autowiring modes are: Autowire.NO, Autowire.BY_NAME and Autowire.BY_TYPE


In next tutorials we will see the examples of each autowiring mode. This series of tutorials (related to @Bean#autowire attribute) are up to version 5.0.20.RELEASE

Note that even though the attribute 'autowire' of @Bean annotation has been removed in Spring 6.0 we can still use this attribute in XML-based configuration with <bean/> element.

See Also