Beans Autowiring

Beans Autowiring

In this tutorial, we are going to discuss beans autowiring in spring framework.

Beans Autowiring

In general, in spring applications, if we want to inject dependent values in setter method dependency injection or constructor dependency injection, we have to use or tags under tag. If we want to inject simple values like primitive and string values, we have to use the “value” attribute. If we’re going to inject Secondary data type elements like Objects, we have to use the “ref” attribute or use the tag in the beans configuration file.

In spring applications, if we want to inject dependent bean objects to another bean object automatically without providing <property> tags and <constructor-arg> tags, then we have to use the Autowiring feature.

The autowiring feature of the spring framework will make the IOC Container to inject dependent objects to the bean objects automatically based on the property names or based on properties types without checking <property> tags and <constructor-arg> tags. There are 3 ways to manage autowiring in Spring applications.

  1. XML Based Autowiring
  2. Annotation Based Autowiring
  3. Auto-Discovery[Stereo Types]
1. XML Based Auto wiring

In this approach, if we want to provide autowiring in spring applications, then we have to use the “autowire” attribute in the <bean> tag.

E.g.

<bean id=”–” class=”–” autowire=”value”>

Here value may be either of the following “autowiring modes”.

  1. no
  2. byName
  3. byType
  4. Constructor
1. no

It is representing “no” autowiring for the bean’s injection, we must provide explicit configuration for the bean’s injection.

2. byName

It will provide autowiring based on the property names. In this autowiring mode, IOC Container will search for dependent bean objects by matching bean properties names with the identity values of the bean’s configuration in the spring configuration file.

3. byType

It will provide autowiring based on the properties data types. In this autowiring mode, IOC Container will identify the dependent bean objects by matching properties data types with the bean data types [class attribute values] in bean configuration.

Note

In the Beans configuration file, only one bean definition must have existed with the same type. If we provide more than one bean configuration with the same type in the beans configuration file, IOC Container will raise an exception.

4. constructor

It is the same as “byType” autowiring mode, but “byType” autowiring will provide setter method dependency injection, and “constructor” autowiring mode will provide constructor dependency injection based on the types.

E.g byName

package com.ashok.spring.core.bean.autowiring.xml.byname.beans;
/**
 * 
 * @author Ashok Kumar
 *
 */
public class Account {
   private String accNo;
   private String accName;
   private String accType;
   private long balance;

   public String getAccNo() {
      return accNo;
   }

   public void setAccNo(String accNo) {
      this.accNo = accNo;
   }

   public String getAccName() {
      return accName;
   }

   public void setAccName(String accName) {
      this.accName = accName;
   }

   public String getAccType() {
      return accType;
   }

   public void setAccType(String accType) {
      this.accType = accType;
   }

   public long getBalance() {
      return balance;
   }

   public void setBalance(long balance) {
      this.balance = balance;
   }

   @Override
   public String toString() {
      return "Account [accNo=" + accNo + ", accName=" + accName + ", accType=" + accType + ", balance=" + balance
            + "]";
   }
}
package com.ashok.spring.core.bean.autowiring.xml.byname.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Address {
   private String street;
   private String city;
   private String state;

   public String getStreet() {
      return street;
   }

   public void setStreet(String street) {
      this.street = street;
   }

   public String getCity() {
      return city;
   }

   public void setCity(String city) {
      this.city = city;
   }

   public String getState() {
      return state;
   }

   public void setState(String state) {
      this.state = state;
   }

   @Override
   public String toString() {
      return "Address [street=" + street + ", city=" + city + ", state=" + state + "]";
   }
}
package com.ashok.spring.core.bean.autowiring.xml.byname.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private Address eaddr;
   private Account eacc;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public Address getEaddr() {
      return eaddr;
   }

   public void setEaddr(Address eaddr) {
      this.eaddr = eaddr;
   }

   public Account getEacc() {
      return eacc;
   }

   public void setEacc(Account eacc) {
      this.eacc = eacc;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", eaddr=" + eaddr + ", eacc=" + eacc + "]";
   }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <bean id="eaddr"
  class="com.ashok.spring.core.bean.autowiring.xml.byname.beans.Address">
  <property name="street" value="APHB Colony" />
  <property name="city" value="Bhimavaram" />
  <property name="state" value="Andhrapradesh" />
 </bean>
 <bean id="eacc"
  class="com.ashok.spring.core.bean.autowiring.xml.byname.beans.Account">
  <property name="accNo" value="12345" />
  <property name="accName" value="Ashok Kumar" />
  <property name="accType" value="Savings" />
  <property name="balance" value="2700" />
 </bean>
 <bean id="emp"
  class="com.ashok.spring.core.bean.autowiring.xml.byname.beans.Employee" autowire="byName">
  <property name="empId" value="E0087" />
  <property name="empName" value="Ashok" />
  <!-- here auto wiring take place
  <property name="eaddr" ref="eaddr"/> 
  <property name="eacc" ref="eacc"/> -->
 </bean>
</beans>
package com.ashok.spring.core.bean.autowiring.xml.byname.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ashok.spring.core.bean.autowiring.xml.byname.beans.Employee;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   @SuppressWarnings("resource")
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/core/bean/autowiring/xml/byname/config/applicationContext.xml";
      ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      Employee emp = (Employee) context.getBean("emp");
      System.out.println(emp);
   }
}

Output

Employee [empName=Ashok, empId=E0087, eaddr=Address [street=APHB Colony, city=Bhimavaram, state=Andhrapradesh], eacc=Account [accNo=12345, accName=Ashok Kumar, accType=Savings, balance=2700]]

E.g.2 ByType

package com.ashok.spring.core.bean.autowiring.xml.bytype.beans;
/**
 * 
 * @author Ashok Kumar
 *
 */
public class Account {
   private String accNo;
   private String accName;
   private String accType;
   private long balance;

   public String getAccNo() {
      return accNo;
   }

   public void setAccNo(String accNo) {
      this.accNo = accNo;
   }

   public String getAccName() {
      return accName;
   }

   public void setAccName(String accName) {
      this.accName = accName;
   }

   public String getAccType() {
      return accType;
   }

   public void setAccType(String accType) {
      this.accType = accType;
   }

   public long getBalance() {
      return balance;
   }

   public void setBalance(long balance) {
      this.balance = balance;
   }

   @Override
   public String toString() {
      return "Account [accNo=" + accNo + ", accName=" + accName + ", accType=" + accType + ", balance=" + balance
            + "]";
   }
}
package com.ashok.spring.core.bean.autowiring.xml.bytype.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Address {
   private String street;
   private String city;
   private String state;

   public String getStreet() {
      return street;
   }

   public void setStreet(String street) {
      this.street = street;
   }

   public String getCity() {
      return city;
   }

   public void setCity(String city) {
      this.city = city;
   }

   public String getState() {
      return state;
   }

   public void setState(String state) {
      this.state = state;
   }

   @Override
   public String toString() {
      return "Address [street=" + street + ", city=" + city + ", state=" + state + "]";
   }
}
package com.ashok.spring.core.bean.autowiring.xml.bytype.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private Address eaddr;
   private Account eacc;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public Address getEaddr() {
      return eaddr;
   }

   public void setEaddr(Address eaddr) {
      this.eaddr = eaddr;
   }

   public Account getEacc() {
      return eacc;
   }

   public void setEacc(Account eacc) {
      this.eacc = eacc;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", eaddr=" + eaddr + ", eacc=" + eacc + "]";
   }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <bean id="eaddr"
  class="com.ashok.spring.core.bean.autowiring.xml.bytype.beans.Address">
  <property name="street" value="APHB Colony" />
  <property name="city" value="Bhimavaram" />
  <property name="state" value="Andhrapradesh" />
 </bean>
 <bean id="eacc"
  class="com.ashok.spring.core.bean.autowiring.xml.bytype.beans.Account">
  <property name="accNo" value="12345" />
  <property name="accName" value="Ashok Kumar" />
  <property name="accType" value="Savings" />
  <property name="balance" value="2700" />
 </bean>
 <bean id="emp"
  class="com.ashok.spring.core.bean.autowiring.xml.bytype.beans.Employee" autowire="byType">
  <property name="empId" value="E0087" />
  <property name="empName" value="Ashok" />
  <!-- here auto wiring take place
  <property name="eaddr" ref="eaddr"/> 
  <property name="eacc" ref="eacc"/> -->
 </bean>
</beans>
package com.ashok.spring.core.bean.autowiring.xml.bytype.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ashok.spring.core.bean.autowiring.xml.bytype.beans.Employee;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   @SuppressWarnings("resource")
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/core/bean/autowiring/xml/bytype/config/applicationContext.xml";
      ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      Employee emp = (Employee) context.getBean("emp");
      System.out.println(emp);
   }
}

Output

Employee [empName=Ashok, empId=E0087, eaddr=Address [street=APHB Colony, city=Bhimavaram, state=Andhrapradesh], eacc=Account [accNo=12345, accName=Ashok Kumar, accType=Savings, balance=2700]]

E.g.3 constructor

package com.ashok.spring.core.bean.autowiring.xml.constructor.beans;
/**
 * 
 * @author Ashok Kumar
 *
 */
public class Account {
   private String accNo;
   private String accName;
   private String accType;
   private long balance;

   public String getAccNo() {
      return accNo;
   }

   public void setAccNo(String accNo) {
      this.accNo = accNo;
   }

   public String getAccName() {
      return accName;
   }

   public void setAccName(String accName) {
      this.accName = accName;
   }

   public String getAccType() {
      return accType;
   }

   public void setAccType(String accType) {
      this.accType = accType;
   }

   public long getBalance() {
      return balance;
   }

   public void setBalance(long balance) {
      this.balance = balance;
   }

   @Override
   public String toString() {
      return "Account [accNo=" + accNo + ", accName=" + accName + ", accType=" + accType + ", balance=" + balance
            + "]";
   }
}
package com.ashok.spring.core.bean.autowiring.xml.constructor.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Address {
   private String street;
   private String city;
   private String state;

   public String getStreet() {
      return street;
   }

   public void setStreet(String street) {
      this.street = street;
   }

   public String getCity() {
      return city;
   }

   public void setCity(String city) {
      this.city = city;
   }

   public String getState() {
      return state;
   }

   public void setState(String state) {
      this.state = state;
   }

   @Override
   public String toString() {
      return "Address [street=" + street + ", city=" + city + ", state=" + state + "]";
   }
}
package com.ashok.spring.core.bean.autowiring.xml.constructor.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private Address eaddr;
   private Account eacc;

   public Employee(String empName, String empId, Address eaddr, Account eacc) {
      super();
      this.empName = empName;
      this.empId = empId;
      this.eaddr = eaddr;
      this.eacc = eacc;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", eaddr=" + eaddr + ", eacc=" + eacc + "]";
   }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

 <bean id="eaddr"
  class="com.ashok.spring.core.bean.autowiring.xml.constructor.beans.Address">
  <property name="street" value="APHB Colony" />
  <property name="city" value="Bhimavaram" />
  <property name="state" value="Andhrapradesh" />
 </bean>
 <bean id="eacc"
  class="com.ashok.spring.core.bean.autowiring.xml.constructor.beans.Account">
  <property name="accNo" value="12345" />
  <property name="accName" value="Ashok Kumar" />
  <property name="accType" value="Savings" />
  <property name="balance" value="2700" />
 </bean>
 <bean id="emp"
  class="com.ashok.spring.core.bean.autowiring.xml.constructor.beans.Employee"
  autowire="constructor">
  <constructor-arg name="empId" value="E0087" />
  <constructor-arg name="empName" value="Ashok Kumar" />
  <!-- here auto wiring take place 
  <property name="eaddr" ref="eaddr"/> 
  <property name="eacc" ref="eacc"/> -->
 </bean>
</beans>
package com.ashok.spring.core.bean.autowiring.xml.constructor.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ashok.spring.core.bean.autowiring.xml.constructor.beans.Employee;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   @SuppressWarnings("resource")
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/core/bean/autowiring/xml/constructor/config/applicationContext.xml";
      ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      Employee emp = (Employee) context.getBean("emp");
      System.out.println(emp);
   }
}

Output

Employee [empName=Ashok Kumar, empId=E0087, eaddr=Address [street=APHB Colony, city=Bhimavaram, state=Andhrapradesh], eacc=Account [accNo=12345, accName=Ashok Kumar, accType=Savings, balance=2700]]

Note

If we want to block any bean object in autowiring, then we have to use the “autowire-candidate” attribute with “false” value in <bean> tag in the beans configuration file.

2. Annotations for Autowiring

To implement Autowiring in Spring applications without providing autowiring configuration in the spring configuration file, we have to use the following annotations provided by the spring framework.

  1. @Required
  2. @Autowired
  3. @Qualifier
1. @Required

This annotation will make IOC Container to inject a particular bean object in another bean object is mandatory. We must use this annotation at the method level, i.e., just before the setXXX() method. After providing this annotation, IOC Container will raise an exception if we do not provide the respective bean injection.

2. @Autowired

We can use Spring @Autowired annotation for spring bean autowiring. @Autowired annotation can be applied to variables and methods for autowiring byType. We can also use @Autowired annotation on the constructor for constructor-based spring autowiring. For @Autowired annotation to work, we also need to enable annotation-based configuration in the spring bean configuration file. This can be done by context:annotation-config element or by defining a bean of type

Note

If we provide the “required” argument value in @Autowired annotation, it is not required to use @Required annotation.

This annotation is following “byType” autowiring internally in spring applications. If we want to use this annotation, then we must have only one bean configuration with the respective type in the configuration file. If we have more than one bean configuration with the same type, IOC Container will raise an exception.

3. @Qualifier

In the case of “byType” autowiring mode, the @Autowired annotation configuration file must provide only one bean configuration with the respective type. If we provide more than one bean configuration with the same type, IOC Container will raise an exception. To resolve the ambiguity of beans injection, we have to use “@Qualifier” annotation in this context. It will specify a particular bean object among the multiple beans of the same type for injection.

E.g. @Qualifier(“bean_Identity”)

package com.ashok.spring.core.bean.autowiring.annotations.beans;
/**
 * 
 * @author Ashok Kumar
 *
 */
public class Account {
   private String accNo;
   private String accName;
   private String accType;
   private long balance;

   public String getAccNo() {
      return accNo;
   }

   public void setAccNo(String accNo) {
      this.accNo = accNo;
   }

   public String getAccName() {
      return accName;
   }

   public void setAccName(String accName) {
      this.accName = accName;
   }

   public String getAccType() {
      return accType;
   }

   public void setAccType(String accType) {
      this.accType = accType;
   }

   public long getBalance() {
      return balance;
   }

   public void setBalance(long balance) {
      this.balance = balance;
   }

   @Override
   public String toString() {
      return "Account [accNo=" + accNo + ", accName=" + accName + ", accType=" + accType + ", balance=" + balance
            + "]";
   }
}
package com.ashok.spring.core.bean.autowiring.annotations.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Address {
   private String street;
   private String city;
   private String state;

   public String getStreet() {
      return street;
   }

   public void setStreet(String street) {
      this.street = street;
   }

   public String getCity() {
      return city;
   }

   public void setCity(String city) {
      this.city = city;
   }

   public String getState() {
      return state;
   }

   public void setState(String state) {
      this.state = state;
   }

   @Override
   public String toString() {
      return "Address [street=" + street + ", city=" + city + ", state=" + state + "]";
   }
}
package com.ashok.spring.core.bean.autowiring.annotations.beans;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee {
   private String empName;
   private String empId;
   
   @Autowired
   private Address eaddr;
   private Account eacc;

   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public Address getEaddr() {
      return eaddr;
   }

   public void setEaddr(Address eaddr) {
      this.eaddr = eaddr;
   }

   public Account getEacc() {
      return eacc;
   }

   @Autowired(required=true)
   @Qualifier("eacc_savings") // If we have more than one bean then using qualifier we can set auto wiring
   public void setEacc(Account eacc) {
      this.eacc = eacc;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", eaddr=" + eaddr + ", eacc=" + eacc + "]";
   }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:annotation-config />
 <bean id="eaddress"
  class="com.ashok.spring.core.bean.autowiring.annotations.beans.Address">
  <property name="street" value="APHB Colony" />
  <property name="city" value="Bhimavaram" />
  <property name="state" value="Andhrapradesh" />
 </bean>
 <bean id="eacc_savings"
  class="com.ashok.spring.core.bean.autowiring.annotations.beans.Account">
  <property name="accNo" value="12345" />
  <property name="accName" value="Ashok Kumar" />
  <property name="accType" value="Savings" />
  <property name="balance" value="2700" />
 </bean>
 <bean id="eacc_current"
  class="com.ashok.spring.core.bean.autowiring.annotations.beans.Account">
  <property name="accNo" value="12346" />
  <property name="accName" value="Ashok Kumar" />
  <property name="accType" value="Current" />
  <property name="balance" value="2100" />
 </bean>
 <bean id="emp"
  class="com.ashok.spring.core.bean.autowiring.annotations.beans.Employee">
  <property name="empId" value="E0087" />
  <property name="empName" value="Ashok" />
 </bean>
</beans>
package com.ashok.spring.core.bean.autowiring.annotations.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ashok.spring.core.bean.autowiring.annotations.beans.Employee;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   @SuppressWarnings("resource")
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/core/bean/autowiring/annotations/config/applicationContext.xml";
      ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      Employee emp = (Employee) context.getBean("emp");
      System.out.println(emp);
   }
}

Output

Employee [empName=Ashok Kumar, empId=E0087, eaddr=Address [street=APHB Colony, city=Bhimavaram, state=Andhrapradesh], eacc=Account [accNo=12345, accName=Ashok Kumar, accType=Savings, balance=2700]]

If we want to use @Autowired annotation for constructor dependency injection then we have to use @Autowired annotation just above of the respective constructor and we have to use @Qualifier annotation along with the Bean parameter in constructor.

package com.ashok.spring.core.bean.autowiring.annotations.constructor.beans;
/**
 * 
 * @author Ashok Kumar
 *
 */
public class Account {
   private String accNo;
   private String accName;
   private String accType;
   private long balance;

   public String getAccNo() {
      return accNo;
   }

   public void setAccNo(String accNo) {
      this.accNo = accNo;
   }

   public String getAccName() {
      return accName;
   }

   public void setAccName(String accName) {
      this.accName = accName;
   }

   public String getAccType() {
      return accType;
   }

   public void setAccType(String accType) {
      this.accType = accType;
   }

   public long getBalance() {
      return balance;
   }

   public void setBalance(long balance) {
      this.balance = balance;
   }

   @Override
   public String toString() {
      return "Account [accNo=" + accNo + ", accName=" + accName + ", accType=" + accType + ", balance=" + balance
            + "]";
   }
}
package com.ashok.spring.core.bean.autowiring.annotations.constructor.beans;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Address {
   private String street;
   private String city;
   private String state;

   public String getStreet() {
      return street;
   }

   public void setStreet(String street) {
      this.street = street;
   }

   public String getCity() {
      return city;
   }

   public void setCity(String city) {
      this.city = city;
   }

   public String getState() {
      return state;
   }

   public void setState(String state) {
      this.state = state;
   }

   @Override
   public String toString() {
      return "Address [street=" + street + ", city=" + city + ", state=" + state + "]";
   }
}
package com.ashok.spring.core.bean.autowiring.annotations.constructor.beans;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class Employee {
   private String empName;
   private String empId;
   private Address eaddr;
   private Account eacc;

   @Autowired
   public Employee(String empName, String empId, Address eaddr, @Qualifier("eacc_current")Account eacc) {
      super();
      this.empName = empName;
      this.empId = empId;
      this.eaddr = eaddr;
      this.eacc = eacc;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + ", eaddr=" + eaddr + ", eacc=" + eacc + "]";
   }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 <context:annotation-config />
 <bean id="eaddress"
  class="com.ashok.spring.core.bean.autowiring.annotations.constructor.beans.Address">
  <property name="street" value="APHB Colony" />
  <property name="city" value="Bhimavaram" />
  <property name="state" value="Andhrapradesh" />
 </bean>
 <bean id="eacc_savings"
  class="com.ashok.spring.core.bean.autowiring.annotations.constructor.beans.Account">
  <property name="accNo" value="12345" />
  <property name="accName" value="Ashok Kumar" />
  <property name="accType" value="Savings" />
  <property name="balance" value="2700" />
 </bean>
 <bean id="eacc_current"
  class="com.ashok.spring.core.bean.autowiring.annotations.constructor.beans.Account">
  <property name="accNo" value="12346" />
  <property name="accName" value="Ashok Kumar" />
  <property name="accType" value="Current" />
  <property name="balance" value="2100" />
 </bean>
 <bean id="emp" class="com.ashok.spring.core.bean.autowiring.annotations.constructor.beans.Employee">
  <constructor-arg value="E0087" />
  <constructor-arg value="Ashok" />
 </bean>
</beans>
package com.ashok.spring.core.bean.autowiring.annotations.constructor.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ashok.spring.core.bean.autowiring.annotations.constructor.beans.Employee;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   @SuppressWarnings("resource")
   public static void main(String[] args) {
      String configFile = "/com/ashok/spring/core/bean/autowiring/annotations/constructor/config/applicationContext.xml";
      ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
      Employee emp = (Employee) context.getBean("emp");
      System.out.println(emp);
   }
}

Output

Employee [empName=E0087, empId=Ashok, eaddr=Address [street=APHB Colony, city=Bhimavaram, state=Andhrapradesh], eacc=Account [accNo=12346, accName=Ashok Kumar, accType=Current, balance=2100]]
3. Auto-Discovery [Stereo Types]

This mechanism will provide the autowiring beans objects without using <bean> configuration in the configuration file. To use this mechanism in Spring applications then we have to use the following annotations provided by the spring framework in the package “org.springframework.stereotype”

  1. @Component: It will represent a component which is recognized by spring Container.
  2. @Repository: It will represent a class as Model Driven, that is, DAO.
  3. @Service: It will represent a class as Service class.
  4. @Controller: It will represent a class as Controller class, it will be used in Spring WEB-MVC Module.

Note

If we want to use these annotations in Spring applications, we must provide the following tag in the spring configuration file.

Syntax

<context:component-scan base-package="---"/>

E.g.

<context:component-scan base-package="com.ashok.spring.service"/>
<context:component-scan base-package="com.ashok.spring.dao"/>
<context:component-scan base-package="com.ashok.spring.controller"/>

Suppose we provide the above tag in the spring configuration file. In that case, IOC Container will scan the specified packages and recognize the annotated classes with @Component, @Repository, @Service, and @Controller. Then, the Container will create bean objects without checking beans configurations in the configuration file.

package com.ashok.spring.core.bean.autowiring.autodiscovery.component.beans;

import org.springframework.stereotype.Component;

/**
 * 
 * @author Ashok Kumar
 *
 */
@Component
public class Employee {
   private String empName;
   private String empId;
   
   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   @Override
   public String toString() {
      return "Employee [empName=" + empName + ", empId=" + empId + "]";
   }
}
package com.ashok.spring.core.bean.autowiring.autodiscovery.component.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.ashok.spring.core.bean.autowiring.autodiscovery.component.beans.Employee;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   public static void main(String[] args) {
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
      context.scan("com.ashok.spring");
      context.refresh();
      Employee emp = context.getBean(Employee.class);
      emp.setEmpId("E0087");
      emp.setEmpName("Ashok Kumar");
      System.out.println(emp);
      context.close();
   }
}

Output

Employee [empName=Ashok Kumar, empId=E0087]

E.g 2

package com.ashok.spring.core.bean.autowiring.autodiscovery.service.beans;

import org.springframework.stereotype.Service;

/**
 * 
 * @author Ashok Kumar
 *
 */
@Service
public class EmployeeSerice {
   private String empName;
   private String empId;
   
   public String getEmpName() {
      return empName;
   }

   public void setEmpName(String empName) {
      this.empName = empName;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   @Override
   public String toString() {
      return "EmployeeSerice [empName=" + empName + ", empId=" + empId + "]";
   }
}
package com.ashok.spring.core.bean.autowiring.autodiscovery.service.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.ashok.spring.core.bean.autowiring.autodiscovery.service.beans.EmployeeSerice;

/**
 * 
 * @author Ashok Kumar
 *
 */
public class TestSpringApplication {
   public static void main(String[] args) {
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
      context.scan("com.ashok.spring");
      context.refresh();
      EmployeeSerice emp = context.getBean(EmployeeSerice.class);
      emp.setEmpId("E0087");
      emp.setEmpName("Ashok Kumar");
      System.out.println(emp);
      context.close();
   }
}

Output

EmployeeService [empName=Ashok Kumar, empId=E0087]
Drawbacks with Autowiring
  1. Autowiring is less exact than normal wiring.
  2. Autowiring will not provide configuration metadata to the documentation tools to prepare Documentations.
  3. Autowiring will increase confusion when more than one bean object of the same type is in the IOC Container.
  4. In Spring applications, if we provide both explicit wiring and autowiring both at a time to a bean object injection, then explicit wiring overrides autowiring configurations.
  5. In Spring configuration files, Explicit wiring will improve readability when compared with autowiring.
  6. In Spring applications, if we have more and more no of bean objects to inject, it is suggestible to use explicit wiring compared with autowiring.
  7. Autowiring is applicable for only bean objects injection, and it is not applicable for simple values injection like primitive values, string values, etc.

Beans Autowiring
Scroll to top