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.

By | 2017-07-21T19:52:11+00:00 March 2nd, 2013|Spring|1 Comment

About the Author:

One Comment

  1. Hoshiyar Singh October 10, 2014 at 7:04 pm - Reply

    post example also. its only theortical and looks like static only.

Leave A Comment