Spring Autowiring

In this article, we will learn about Spring framework’s autowiring feature. Instead of explicitly wiring between the collaborating beans, spring feature allows you to do autowiring in various ways. This means that it is possible that spring automatically resolves relationship between the collaborating beans.

To use this feature in Spring Application, enable it by adding “autowire” attribute under <bean> element.

For example,
<bean id=“employee” class=“com.spring.series.core.Employee” autowire=“byName”>

Note: bean dependencies are also referred as ‘collaborating beans’ or simply collaborators.

Ways to achieve Autowiring in Spring

  • XML based configuration
  • Annotation

Autowiring modes

Mode Explanation
no autowire=”no”Default behavior, this is what we have implemented in the earlier articles <link1> <link2>.
Sometimes, it is called as explicit bean wiring.
Use ‘ref’ attribute to wire the collaborating beans
byName autowire=”byName”This option injects the collaborating beans using bean names defined in the Spring configuration file. If the beanName is found in the configuration file, then its get injected otherwise fatal error is raised.
byType autowire=”byType”This option searches the collaborating beans using the bean types in the Spring configuration file. If the matching bean type is found in the configuration file, then its get injected otherwise a fatal error is raised.Note: if there are two similar bean types available in the configuration file, then it throws “NoUniqueBeanDefinitionException” excpetion.
constructor autowire=”constructor”Similar to ‘byType’ but this applies to constructor arguments. If there isn’t one matching constructor, then a fatal error is raised.
auto-detect autowire=”constructor”It first searches collaborating beans using ‘constructor’, if there isn’t one matching constructor available then it autowires using ‘byType’.Deprecated as of Spring 3.x see here

Advantages of using Spring Autowiring

  • Greatly reduces amount of code to be configured in the configuration XML file
  • Use of <property> & <constructor-arg> tags can be eliminated.

Disadvantages

  • Readability decreases, although it reduces amount of code in the configuration file
  • Maintenance becomes difficult
  • <property> & <constructor-arg> tags always overrides autowiring
  • If there are two or more similar type beans defined in the spring configuration file, then while autowiring byType causes ambiguities and throws “NoUniqueBeanDefinitionException” exception
  • Autowiring of simple types i.e.; primitives (int, float, double, etc) aren’t possible

 Autowiring Examples

As we got a brief idea about Autowiring concepts in Spring Framework, so let’s move on to see each mode example one by one.

  1. no
  2. byName
  3. byType
  4. constructor
  5. auto-detect – this is not implemented, as it is deprecated from Spring 3.x see here

Conclusion: Although, Spring autowiring concept greatly reduces the amount of code it causes ambiguity and readability. So, one should go for explicit bean wiring instead of autowiring in XML based configuration. But with Annotation, autowiring got simple improving the readability of the code

Happy Coding !!
Happy Learning !!

Spring Autowiring using byName
Spring Dependency Injection using Constructor