Monday, August 31, 2015

Autowiring without Annotation

INTRODUCTION

Spring Framework enables injecting our beans by without mentioning the dependencies with the help of auto-wiring. No need to care about the bean's properties suppose if it is another bean.

There are three ways to achieve auto-wiring in spring framework. They are
  1. By Name
  2. By Type
  3. By Constructor

In this post, I will provide examples for all these 3 types.

EXAMPLE

Create a POJO for Person.

package springcore.autowiring;

public class Person {
 private Address address;
 private Education education;

 public Address getAddress() {
   return address;
 }

 public void setAddress(Address address) {
   this.address = address;
 }

 public Education getEducation() {
   return education;
 }

 public void setEducation(Education education) {
   this.education = education;
 }
}

Create another POJO for Address.

package springcore.autowiring;

public class Address {

 private String streeName;
 private String doorNo;

 public String getStreeName() {
   return streeName;
 }

 public void setStreeName(String streeName) {
   this.streeName = streeName;
 }

 public String getDoorNo() {
   return doorNo;
 }

 public void setDoorNo(String doorNo) {
   this.doorNo = doorNo;
 }
}

Create another POJO for Education.

package springcore.autowiring;

public class Education {

 private String qualification;
 private String institute;

 public String getQualification() {
   return qualification;
 }

 public void setQualification(String name) {
   this.qualification = name;
 }

 public String getInstitute() {
   return institute;
 }

 public void setInstitute(String institute) {
   this.institute = institute;
 }

}

Now configure the beans in beans configuration xml (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="address" class="springcore.autowiring.Address">
 <property name="streeName" value="Gandhi Street" />
   <property name="doorNo" value="15" />
</bean>

<bean id="education" class="springcore.autowiring.Education">
  <property name="qualification" value="M.Tech" />
  <property name="institute" value="SRM University" />
</bean>

</beans>

Now time to test the auto-wiring. Create the main program and call the beans.

package springcore.autowiring;

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

public class PersonMain {

 ApplicationContext applicationContext;
 Person person;

 PersonMain() {
   applicationContext = new FileSystemXmlApplicationContext(
       "C:/config/autowiring-beans.xml");

   person = (Person) applicationContext.getBean("person");
 }

 public Address getAddress() {
   return person.getAddress();
 }

 public static void main(String[] args) {
   PersonMain personMain = new PersonMain();
   System.out.println("Person Address is: " + personMain.getAddress().getStreeName());
 }
}

AUTO-WIRING TYPES

  1. Auto-wiring by Name
    When we apply auto-wiring by name, spring container will look for the matching bean Name and inject it.
<bean id="person" class="springcore.autowiring.Person"
autowire="byName" />

  1. Auto-wiring by Type
    When we apply auto-wiring by type, spring container will look for the matching bean and inject it.
<bean id="person" class="springcore.autowiring.Person"
autowire="byType" />

  1. Auto-wiring by Constructor
    When we apply auto-wiring by a constructor, the spring container will look for the constructor of the bean and inject it.
<bean id="person" class="springcore.autowiring.Person"
autowire="constructor" />

Hope this post helps to understand the auto-wiring without annotation. I will cover the auto-wiring with annotation in another post.
Please share your comments and suggestions.
Happy Knowledge Sharing!!!

No comments :

Post a Comment