Pin It

How to perform Autowiring in Spring Framework using Annotations ?

Java Development Spring Framework : A simple program demonstrating how to perform Autowiring using Annotations.

Auto-wiring in Spring Framework using Annotation can be done by three possible ways :
1. Applying Annotation @Autowired over the property itself
2. Applying Annotation @Autowired over the setter method of property
3. Applying Annotation @Autowired over the constructor

All the three Scenarios are covered below.

In order to run each scenario individually we just change Employee.java having annotations at different places and rest code remains the same. So for each scenario to run individual we need

1. Scenario based Employee POJO class which has-a Address class object
2. Address POJO class which will be wired to Employee class through auto-wiring
3. Test class to test the application
4. Spring Configuration Xml file.


Address POJO class -

package com.hubberspot.spring.autowire;


public class Address {

 private String street;
 private String city;
 private String state;

 public Address() {

 }


 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;
 }

}




Spring Configuration XML file - 


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.

<?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.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">

 <!-- In order to turn on Annotations in Spring over xml configuration we 
  need to provide a tag by name <context:annotation-config> see the tag below 
  .... -->

 <context:annotation-config />

 <bean id="employee" class="com.hubberspot.spring.autowire.Employee">
  <property name="name" value="Jonty" />
  <property name="age" value="28" />
 </bean>

 <bean id="address" class="com.hubberspot.spring.autowire.Address">

  <property name="street" value="Town Hall Street" />
  <property name="city" value="Pune" />
  <property name="state" value="Maharashtra"></property>
 </bean>

</beans>



Test class -

package com.hubberspot.spring.autowire;

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

public class Test {

 public static void main(String[] args) {

  // ApplicationContext is a Spring interface which 
  // provides with the configuration for an application. 
  // It provides us with all the methods that BeanFactory 
  // provides. It loads the file resources in a older 
  // and generic manner. It helps us to publish events to the
  // listener registered to it. It also provides quick support
  // for internationalization. It provides us with the object 
  // requested, it reads the configuration file and provides
  // us with the necessary object required.
  // We are using concrete implementation of ApplicationContext
  // here called as ClassPathXmlApplicationContext because this 
  // bean factory reads the xml file placed in the classpath of 
  // our application. We provide ClassPathXmlApplicationContext
  // with a configuration file called as spring.xml placed
  // at classpath of our application. 
  ApplicationContext context = 
    new ClassPathXmlApplicationContext(("spring.xml"));

  // In order to get a object instantiated for a particular bean 
  // we call getBean() method of ClassPathXmlApplicationContext
  // passing it the id for which the object is to be needed. 
  // Here getBean() returns an Object. We need to cast it back 
  // to the Employee object. Without implementing new keyword we 
  // have injected object of Employee just by reading an xml 
  // configuration file.
  Employee employee = (Employee)context.getBean("employee");

  System.out.println("The name of Employee is : " + employee.getName());
  System.out.println("The age of Employee is : " + employee.getAge());
  System.out.println("The address of Employee is : " + 
    employee.getAddress().getStreet() +" "+
    employee.getAddress().getCity() +" "+
    employee.getAddress().getState());

 }

}




Scenario 1 : Employee.java class having annotation at Property level
package com.hubberspot.spring.autowire;

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

 // Create a POJO class Employee which has a 
 // Address Object reference as instance variable
 public class Employee {

  private String name;
  private int age;

  // Annotation based autowiring of bean
  // Using annotation as @Autowired from the package 
  // org.springframework.beans.factory.annotation.Autowired
  // Here its Autowire by property as we are directly
  // applying @Autowired annotation over the property

  @Autowired
  private Address address;

  public Employee() {

  }

  public String getName() {
   return name;
  }

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

  public int getAge() {
   return age;
  }

  public void setAge(int age) {
   this.age = age;
  }

  public Address getAddress() {
   return address;
  }

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

 }




Scenario 2 : Employee.java class having annotation at Mehtod level
package com.hubberspot.spring.autowire;

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

// Create a POJO class Employee which has a 
// Address Object reference as instance variable
public class Employee {

 private String name;
 private int age;
 private Address address;

 public Employee() {

 }

 public String getName() {
  return name;
 }

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

 public int getAge() {
  return age;
 }

 public void setAge(int age) {
  this.age = age;
 }

 public Address getAddress() {
  return address;
 }


 // Annotation based autowiring of bean
 // Using annotation as @Autowired from the package 
 // org.springframework.beans.factory.annotation.Autowired
 // Here its Autowire by setter method as we are directly
 // applying @Autowired annotation over the setter method

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

}




Scenario 3 : Employee.java class having annotation at Constructor level
package com.hubberspot.spring.autowire;

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

 // Create a POJO class Employee which has a 
 // Address Object reference as instance variable
 public class Employee {

  private String name;
  private int age;
  private Address address;


  // Annotation based autowiring of bean
  // Using annotation as @Autowired from the package 
  // org.springframework.beans.factory.annotation.Autowired
  // Here its Autowire by constructor as we are directly
  // applying @Autowired annotation over the constructor

  @Autowired
  public Employee( Address address) {

   this.address = address;  
  }

  public String getName() {
   return name;
  }

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

  public int getAge() {
   return age;
  }

  public void setAge(int age) {
   this.age = age;
  }

  public Address getAddress() {
   return address;
  }

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

 }




Output of all the three scenarios would be : 


 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot