Spring Autowiring

Spring container autowire relationship between different beans. Whenever we create a bean in Spring (In configuration file). Spring will check the dependency of that bean and inject the dependency @ run-time  Spring will identify @ run-time which bean to inject by different mechanism, It is called Autowiring.

Advantage of Autowiring

  • Reduce the development time by removing the necessity of specifying properties and constructor argument.

Enable Autowiring

XML Based Metadata Configuration

Autowiring can be enabled by using autowire attribute in <bean/> element.

Annotation Based Metadata Configuration

Autowiring can be enabled by using @Autowire annotation

Autowiring Modes

Autowiring in spring has 5 different modes.

  • No                         :  No Autowiring will be done for given bean. ref element should be used to specify the bean to inject.
  • byName           :  Autowiring by Property Name
  • byType             :  Allows a property to be autowired if exactly one bean of the property type exists in the container.
  • constructor  :  Analogous to byType, but applies to constructor arguments.

 

Limitation of Autowiring

  • Explicit dependencies in property and constructor-arg settings always override autowiring.
  • Autowiring is less exact than explicit wiring.
  • Wiring information may not be available to tools that may generate documentation from a Spring container.
  • If no unique bean definition is available, an exception is thrown.

 

Disable Autowiring for a particular Bean.

Use autowire-candidate and set it as FALSE.

1 Comment Spring Autowiring

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.