Autowiring in Spring Container

The Spring container can autowire relationships between collaborating beans. It can be implement either in configuration file or through annotation @Autowired. The Container automatically creates related object whenever required.

The below are advantages of autowiring :
  • Reduces the need to specify properties or constructor arguments
  • New dependencies can be added to a class without changing the configuration
Autowiring modes :
Modes Description
no No autowiring means bean references must be defined via a ref element.
byName Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired.
byType Allows a property to be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for that bean
constructor It is same as byType, but applies to constructor arguments. If there is not exactly one matching constructor argument type in the container, a fatal error is raised.
autodetect If a default constructor with no argument is found, the dependencies will be auto-wired by type. Otherwise, they will be auto-wired by constructor

Examples

Create POJO classes :
Address.java
package techmyguru.spring.autowiring;
public class Address {
  private String street, city, zip;
  // all getter and setter methods
  
  @Override
  public String toString() {
    return street + "\t" + city + "\t" + zip;
  }
}

Employee.java
package techmyguru.spring.autowiring;
public class Employee {
  private String eid, name;
  private double salary;
  private Address address;
  // all getter and setter methods
  @Override
  public String toString() {
    return eid + "\t" + name + "\t" + salary+"\t"+address;
  }
}

Add Spring library in project classpath : Add the following jar files.
spring-beans-3.2.4.RELEASE.jar
spring-build-src-3.2.4.RELEASE.jar
spring-context-3.2.4.RELEASE.jar
spring-context-support-3.2.4.RELEASE.jar
spring-core-3.2.4.RELEASE.jar
spring-expression-3.2.4.RELEASE.jar
spring-test-3.2.4.RELEASE.jar
spring-tx-3.2.4.RELEASE.jar
commons-logging-1.0.4.jar

Create EmployeeClient.java to create Container
package techmyguru.spring.autowiring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class EmployeeClient {
  public static void main(String[] args) {
  ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:techmyguru/spring/autowiring/beans.xml");
  Employee emp1=(Employee)ctx.getBean("emp");
  System.out.println(emp1);
  }
}


Verify the directory structure :

nowiring

Create Spring configuration file : techmyguru\spring\autowiring\beans.xml
<?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.xsd">
<bean id="add" class="techmyguru.spring.autowiring.Address" >
    <property name="street" value="Vikash Marg" />
    <property name="city" value="Delhi" />
    <property name="zip" value="1234567" />
  </bean>
  <bean id="emp" class="techmyguru.spring.autowiring.Employee" autowire="no">
    <property name="eid" value="A005" />
    <property name="name" value="Minakshi Kumari" />
    <property name="salary" value="666666" />
    <property name="address">
      <ref bean="add"/>
    </property>
  </bean>
</beans>

Run the EmployeeClient.java and get output :


Download Source Code

byName

Change the configuration file as below : techmyguru\spring\autowiring\beans.xml

<bean id="emp" class="techmyguru.spring.autowiring.Employee"  autowire= "byName" >
...................................................
  </bean>

Run the EmployeeClient.java and get output :


Download Source Code

byType

Change the configuration file as below : techmyguru\spring\autowiring\beans.xml

<bean id="emp" class="techmyguru.spring.autowiring.Employee"  autowire= "byType" >
...................................................
  </bean>

Run the EmployeeClient.java and get output :


Download Source Code

constructor

Change Employee.java as below -
package techmyguru.spring;
public class Employee {
  private String eid, name;
  private double salary;
  private Address address;
  public Employee(Address address) {
    this.address = address;
  }
  // all getter and setter mehods
  @Override
  public String toString() {
    return eid + "\t" + name + "\t" + salary + "\t" + address;
  }
}

Change the configuration file as below : techmyguru\spring\autowiring\beans.xml

<bean id="emp" class="techmyguru.spring.autowiring.Employee"  autowire= "constructor" >
...................................................
  </bean>

Run the EmployeeClient.java and get output :


Download Source Code
Previous   Next


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code