Annotation base Autowiring Example in Spring

Tuesday, June 8, 2010

As you may be aware one of the highlight of Spring is autowiring. Autowiring means, beans are injected with proper dependency objects without explicit declaration. This is done by the Spring container based on information available to it.

For autowiring to take place, Spring container needs to be notified. It can happen either from configuration file which is present in form of xml, or Java base autowiring where in annotations is used.

In this example I am showing how annotation can be used in Java code to support autowiring.

Take a scenario where in there is a Customer class which has dependency on two other classes, which are Identity, which contains customer name etc. and Address class which maintains customer address information. In the Customer class we declare the autowiring to container using the @Autowire annotation on the constructor. This means that the container will supply appropriate beans for the constructor parameters from the context.

We declare two other classes Identity and Address which are simple Java POJO classes.

In the configuration file we mention the tag which will allow container to look for annotations.



Here are the source codes.

1. Code for Customer.java class. Notice the @Autowired annotation on the constructor.


package headfirstspring;

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

public class Customer {

private Address address;
private Identity identity;

@Autowired
public Customer(Identity identity, Address address) {
this.address = address;
this.identity = identity;
}

public void showStateInfo(){
System.out.println(identity.getFirstName() + " stays in "+address.getState() + " state.");
}
}



2. Here is the code for Identity.java


package headfirstspring;

public class Identity {
private String firstName;
private String lastName;
private String ssn;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
}



3. Here is the Address.java


package headfirstspring;

public class Address {
private String street;
private String state;
private String zip;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
}


4. Here is the config.xml. If you look carefully we do not mention the dependencies of the Customer in this file here. We simply declare the customer bean

















5. Finally here is the test program TestAutoWiring.java


package headfirstspring;

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

public class TestAutoWiring {
public static void main(String args[]){
// create and configure beans
ApplicationContext context =
new ClassPathXmlApplicationContext("config.xml");
// retrieve configured instance
Customer customer = (Customer)context.getBean("customer");
customer.showStateInfo();
}
}

0 comments:

Post a Comment

  © Blogger template On The Road by Ourblogtemplates.com 2009

Back to TOP