Spring Setter dependency injection using annotation

Here we will learn about the Setter Injection in Spring using Annotation.

Company Bean Class

Company.java

package bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/*
 *  This Annotation enable to scan this class
 *  Value will decide the ID of create bean. This ID can be used to get the bean of this class
 */
@Component(value = "company1")
public class Company {

	@Autowired
	@Value("VGautam")
	String companyCEO;

	@Autowired(required = true)
	// @Qualifier("Address")
	Address companyAddress;

	public String getCompanyCEO() {
		return companyCEO;
	}

	public void setCompanyCEO(String companyCEO) {
		this.companyCEO = companyCEO;
	}

	public Address getCompanyAddress() {
		return companyAddress;
	}

	public void setCompanyAddress(Address companyAddress) {
		this.companyAddress = companyAddress;
	}

	@Override
	public String toString() {
		return "Company [companyCEO=" + companyCEO + ", companyAddress="
				+ companyAddress + "]";
	}

}

Address Bean Class

Address.java

package bean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Address {

	@Autowired(required=true)
	@Value("201")
	public String flatNo;

	@Autowired(required=true)
	@Value("2")
	public String bldgNo;

	@Autowired
	@Value("Street No")
	public String streetNo;

	@Autowired
	@Value("Mumbai")
	public String city;

	@Autowired
	@Value("123456")
	public int pincode;

	public String getStreetNo() {
		return streetNo;
	}

	public void setStreetNo(String streetNo) {
		System.out.println("Setter method is called");
		this.streetNo = streetNo;
	}

	public String getFlatNo() {
		return flatNo;
	}

	public void setFlatNo(String flatNo) {
		this.flatNo = flatNo;
	}

	public String getBldgNo() {
		return bldgNo;
	}

	public void setBldgNo(String bldgNo) {
		this.bldgNo = bldgNo;
	}

	public String getCity() {
		return city;
	}

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

	public int getPincode() {
		return pincode;
	}

	public void setPincode(int pincode) {
		this.pincode = pincode;
	}

	/*
	 * No-Argument Constructor or No-Argument Static factory method is required
	 * to make setter injection work. In case none of them exist(No arg
	 * Constructor/Static method) "No default constructor found;" error will be
	 * thrown
	 */
	private Address() {
		super();
		System.out.println("Invoking No Arg Constructor");
	}

	@Override
	public String toString() {
		return "Address [flatNo=" + flatNo + ", bldgNo=" + bldgNo
				+ ", streetNo=" + streetNo + ", city=" + city + ", pincode="
				+ pincode + "]";
	}

}

Main Class

package main;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import bean.Address;
import bean.Company;

public class Main {

	public static void main(String[] args) {

		AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext();

		/*
		 * Below line will scan the given package for Components. It will be
		 * refreshed then to get bean from the same.
		 */
		annotationContext.scan("bean");
		annotationContext.refresh();

		/*
		 * Here Class name is used to get the object of Address class
		 */
		Address address = annotationContext.getBean(Address.class);

		/*
		 * Here ID name is used to get the Object of Company Class
		 */
		Company company = (Company) annotationContext.getBean("company1");

		System.out.println("Address Object :::::" + address.toString());

		System.out.println("Company Object :::::" + company.toString());

	}

}

Output of the above programe

Invoking No Arg Constructor
Address Object :::::Address [flatNo=201, bldgNo=2, streetNo=Street No, city=Mumbai, pincode=123456]
Company Object :::::Company [companyCEO=VGautam, companyAddress=Address [flatNo=201, bldgNo=2, streetNo=Street No, city=Mumbai, pincode=123456]]
By | 2013-09-19T04:34:42+00:00 September 19th, 2013|Spring|1 Comment

About the Author:

One Comment

  1. krishna November 30, 2015 at 6:21 am - Reply

    Iam getting ClassNotFoundException while run this application
    how to solve it

Leave A Comment