URI:
   DIR Return Create A Forum - Home
       ---------------------------------------------------------
       Java
  HTML https://java.createaforum.com
       ---------------------------------------------------------
       *****************************************************
   DIR Return to: General Discussion
       *****************************************************
       #Post#: 7--------------------------------------------------
       Spring Bean Autowiring
       By: shekar Date: September 15, 2014, 7:57 am
       ---------------------------------------------------------
       Wiring a bean means configuring a bean along with its
       dependencies into an xml file.
       by default autowiring is disabled in spring framework.
       It means the programmer has to explicitly wire the bean
       properties into an xml file.
       If autowiring is enabled then spring container will take care
       about injecting the dependencies, programmer no need to
       configure into an xml file explicitly.
       Autowiring is only supported if the dependancies are in the form
       of objects only.
       To enable autowiring, we should add autowire attribute to the
       bean element [or] bean tag.
       In Spring, 5 Auto-wiring modes are supported.
       1.no – Default, no auto wiring, set it manually via “ref”
       attribute
       2.byName – Auto wiring by property name. If the name of a bean
       is same as the name of other bean property, auto wire it.
       3.byType – Auto wiring by property data type. If data type of a
       bean is compatible with the data type of other bean property,
       auto wire it.
       4.constructor – byType mode in constructor argument.
       5.autodetect – If a default constructor is found, use “autowired
       by constructor”; Otherwise, use “autowire by type”
       Example:
       <bean id="customer" class="com.mkyong.common.Customer"
       autowire="byName" />
       A Customer and Person object for auto wiring demonstration.
       public class Customer
       {
       private Person person;
       
       public Customer(Person person) {
       this.person = person;
       }
       
       public void setPerson(Person person) {
       this.person = person;
       }
       //...
       }
       public class Person
       {
       //...
       }
       1. Auto-Wiring ‘no’
       This is the default mode, you need to wire your bean via ‘ref’
       attribute.
       <bean id="customer" class="Customer">
       <property name="person" ref="person" />
       </bean>
       
       <bean id="person" class="Person" />
       2. Auto-Wiring ‘byName’
       Auto-wire a bean by property name. In this case, since the name
       of “person” bean is same with the name of the “customer” bean’s
       property (“person”), so, Spring will auto wired it via setter
       method – “setPerson(Person person)“.
       In Spring, “Autowiring by Name” means, if the name of a bean is
       same as the name of other bean property, auto wire it.
       For example, if a “customer” bean exposes an “address” property,
       Spring will find the “address” bean in current container and
       wire it automatically. And if no matching found, just do
       nothing.
       You can enable this feature via autowire="byName" like below :
       <bean id="customer" class="Customer" autowire="byName" />
       
       <bean id="address" class="Address" >
       <property name="fulladdress" value="Block A 888, CA" />
       </bean>
       Two beans, customer and address.
       public class Customer
       {
       private Address address;
       //...
       }
       public class Address
       {
       private String fulladdress;
       //...
       }
       In Spring, “Autowiring by Name” means, if the name of a bean is
       same as the name of other bean property, auto wire it.
       For example, if a “customer” bean exposes an “address” property,
       Spring will find the “address” bean in current container and
       wire it automatically. And if no matching found, just do
       nothing.
       You can enable this feature via autowire="byName" like below :
       <!-- customer has a property name "address" -->
       <bean id="customer" class="com.mkyong.common.Customer"
       autowire="byName" />
       
       <bean id="address" class="com.mkyong.common.Address" >
       <property name="fulladdress" value="Block A 888, CA" />
       </bean>
       See a full example of Spring auto wiring by name.
       1. Beans
       Two beans, customer and address.
       package com.mkyong.common;
       
       public class Customer
       {
       private Address address;
       //...
       }
       package com.mkyong.common;
       
       public class Address
       {
       private String fulladdress;
       //...
       }
       2. Spring Wiring
       Normally, you wire the bean explicitly, via ref attribute like
       this :
       <bean id="customer" class="Customer" >
       <property name="address" ref="address" />
       </bean>
       
       <bean id="address" class="Address" >
       <property name="fulladdress" value="Block A 888, CA" />
       </bean>
       With autowire by name enabled, you do not need to declares the
       property tag anymore. As long as the “address” bean is same name
       as the property of “customer” bean, which is “address”, Spring
       will wire it automatically.
       <bean id="customer" class="Customer" autowire="byName" />
       
       <bean id="address" class="Address" >
       <property name="fulladdress" value="Block A 888, CA" />
       </bean>
       See another example, this time, the wiring will failed, caused
       the bean “addressABC” is not match the property name of bean
       “customer”.
       <bean id="customer" class="Customer" autowire="byName" />
       
       <bean id="addressABC" class="Address" >
       <property name="fulladdress" value="Block A 888, CA" />
       </bean>
       3.Spring Autowiring by Type:
       In Spring, “Autowiring by Type” means, if data type of a bean is
       compatible with the data type of other bean property, auto wire
       it.
       For example, a “person” bean exposes a property with data type
       of “ability” class, Spring will find the bean with same data
       type of class “ability” and wire it automatically. And if no
       matching found, just do nothing.
       You can enable this feature via autowire="byType" like below :
       <bean id="person" class="Person" autowire="byType" />
       
       <bean id="invisible" class="Ability" >
       <property name="skill" value="Invisible" />
       </bean>
       1. Beans
       Two beans, person and ability.
       public class Person
       {
       private Ability ability;
       //...
       }
       public class Ability
       {
       private String skill;
       //...
       }
       2. Spring Wiring
       Normally, you wire the bean explicitly :
       <bean id="person" class="Person">
       <property name="ability" ref="invisible" />
       </bean>
       
       <bean id="invisible" class="Ability" >
       <property name="skill" value="Invisible" />
       </bean>
       With autowire by type enabled, you can leave the ability
       property unset. Spring will find the same data type and wire it
       automatcailly.
       <bean id="person" class="Person" autowire="byType" />
       
       <bean id="invisible" class="Ability" >
       <property name="skill" value="Invisible" />
       </bean>
       what if you have two beans with same data type of class
       “ability”?
       <bean id="person" class="com.mkyong.common.Person"
       autowire="byType" />
       
       <bean id="steal" class="Ability" >
       <property name="skill" value="Steal" />
       </bean>
       
       <bean id="invisible" class="Ability" >
       <property name="skill" value="Invisible" />
       </bean>
       Exception in thread "main"
       org.springframework.beans.factory.UnsatisfiedDependencyException
       :
       ...
       No unique bean of type [Ability] is defined:
       expected single matching bean but found 2: [steal, invisible];
       nested exception is
       org.springframework.beans.factory.NoSuchBeanDefinitionException:
       No unique bean of type [Ability] is defined:
       expected single matching bean but found 2: [steal, invisible]
       In Spring framework, you can wire beans automatically with
       auto-wiring feature. To enable it, just define the “autowire”
       attribute in <bean>.
       <bean id="customer" class="com.mkyong.common.Customer"
       autowire="byName" />
       In Spring, 5 Auto-wiring modes are supported.
       no – Default, no auto wiring, set it manually via “ref”
       attribute
       byName – Auto wiring by property name. If the name of a bean
       is same as the name of other bean property, auto wire it.
       byType – Auto wiring by property data type. If data type of
       a bean is compatible with the data type of other bean property,
       auto wire it.
       constructor – byType mode in constructor argument.
       autodetect – If a default constructor is found, use
       “autowired by constructor”; Otherwise, use “autowire by type”.
       Examples
       A Customer and Person object for auto wiring demonstration.
       package com.mkyong.common;
       
       public class Customer
       {
       private Person person;
       
       public Customer(Person person) {
       this.person = person;
       }
       
       public void setPerson(Person person) {
       this.person = person;
       }
       //...
       }
       package com.mkyong.common;
       
       public class Person
       {
       //...
       }
       1. Auto-Wiring ‘no’
       This is the default mode, you need to wire your bean via ‘ref’
       attribute.
       <bean id="customer" class="com.mkyong.common.Customer">
       <property name="person" ref="person" />
       </bean>
       
       <bean id="person" class="com.mkyong.common.Person" />
       2. Auto-Wiring ‘byName’
       Auto-wire a bean by property name. In this case, since the name
       of “person” bean is same with the name of the “customer” bean’s
       property (“person”), so, Spring will auto wired it via setter
       method – “setPerson(Person person)“.
       <bean id="customer" class="com.mkyong.common.Customer"
       autowire="byName" />
       
       <bean id="person" class="com.mkyong.common.Person" />
       See full example – Spring Autowiring by Name.
       3. Auto-Wiring ‘byType’
       Auto-wire a bean by property data type. In this case, since the
       data type of “person” bean is same as the data type of the
       “customer” bean’s property (Person object), so, Spring will auto
       wired it via setter method – “setPerson(Person person)“.
       <bean id="customer" class="com.mkyong.common.Customer"
       autowire="byType" />
       
       <bean id="person" class="com.mkyong.common.Person" />
       See full example – Spring Autowiring by Type.
       4. Auto-Wiring ‘constructor’
       Auto-wire a bean by property data type in constructor argument.
       In this case, since the data type of “person” bean is same as
       the constructor argument data type in “customer” bean’s property
       (Person object), so, Spring auto wired it via constructor method
       – “public Customer(Person person)“.
       In Spring, “Autowiring by Constructor” is actually autowiring by
       Type in constructor argument. It means, if data type of a bean
       is same as the data type of other bean constructor argument,
       auto wire it.
       1. Beans
       Two beans, developer and language.
       public class Developer {
       private Language language;
       
       //autowire by constructor
       public Developer(Language language) {
       this.language = language;
       }
       
       //...
       
       }
       public class Language {
       private String name;
       //...
       }
       2. Spring Wiring
       Normally, you wire the bean via constructor like this :
       <bean id="developer" class="Developer">
       <constructor-arg>
       <ref bean="language" />
       </constructor-arg>
       </bean>
       
       <bean id="language" class="Language" >
       <property name="name" value="Java" />
       </bean>
       With autowire by constructor enabled, you can leave the
       constructor property unset. Spring will find the compatible data
       type and wire it automatcailly.
       <bean id="developer" class="Developer" autowire="constructor"
       />
       
       <bean id="language" class="Language" >
       <property name="name" value="Java" />
       </bean>
       In Spring, “Autowiring by AutoDetect“, means chooses “autowire
       by constructor” if default constructor (argument with any data
       type), otherwise uses “autowire by type“.
       See an example of Spring “auto wiring by autodetect”. Auto
       wiring the “kungfu” bean into “panda”, via constructor or type
       (base on the implementation of panda bean).
       <bean id="panda" class="Panda" autowire="autodetect" />
       
       <bean id="kungfu" class="KungFu" >
       <property name="name" value="Shao lin" />
       </bean>
       1. AutoDetect – by Constructor
       If a default constructor is supplied, auto detect will chooses
       wire by constructor.
       public class Panda {
       private KungFu kungfu;
       
       public Panda(KungFu kungfu) {
       System.out.println("autowiring by constructor");
       this.kungfu = kungfu;
       }
       
       public KungFu getKungfu() {
       return kungfu;
       }
       
       public void setKungfu(KungFu kungfu) {
       System.out.println("autowiring by type");
       this.kungfu = kungfu;
       }
       
       //...
       }
       In Spring, “Autowiring by AutoDetect“, means chooses “autowire
       by constructor” if default constructor (argument with any data
       type), otherwise uses “autowire by type“.
       See an example of Spring “auto wiring by autodetect”. Auto
       wiring the “kungfu” bean into “panda”, via constructor or type
       (base on the implementation of panda bean).
       <bean id="panda" class="com.mkyong.common.Panda"
       autowire="autodetect" />
       
       <bean id="kungfu" class="com.mkyong.common.KungFu" >
       <property name="name" value="Shao lin" />
       </bean>
       1. AutoDetect – by Constructor
       If a default constructor is supplied, auto detect will chooses
       wire by constructor.
       package com.mkyong.common;
       
       public class Panda {
       private KungFu kungfu;
       
       public Panda(KungFu kungfu) {
       System.out.println("autowiring by constructor");
       this.kungfu = kungfu;
       }
       
       public KungFu getKungfu() {
       return kungfu;
       }
       
       public void setKungfu(KungFu kungfu) {
       System.out.println("autowiring by type");
       this.kungfu = kungfu;
       }
       
       //...
       }
       Output
       autowiring by constructor
       Person [kungfu=Language [name=Shao lin]]
       2. AutoDetect – by Type
       If a default constructor is not found, auto detect will chooses
       wire by type.
       public class Panda {
       private KungFu kungfu;
       
       public KungFu getKungfu() {
       return kungfu;
       }
       
       public void setKungfu(KungFu kungfu) {
       System.out.println("autowiring by type");
       this.kungfu = kungfu;
       }
       
       //...
       }
       *****************************************************