Here we will learn about the Setter Injection in Spring using Annotation.
Company Bean Class
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
public Company(@Value("VGautam") String companyCEO, Address companyAddress) {
super();
this.companyCEO = companyCEO;
this.companyAddress = companyAddress;
}
String companyCEO;
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
public Address(@Value("Flat No") String flatNo,@Value("2B") String bldgNo, @Value("Street No") String streetNo,
@Value("City") String city,@Value("123456") int pincode) {
super();
this.flatNo = flatNo;
this.bldgNo = bldgNo;
this.streetNo = streetNo;
this.city = city;
this.pincode = pincode;
}
public String flatNo;
public String bldgNo;
public String streetNo;
public String city;
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
Main.java
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 above program
Address Object :::::Address [flatNo=Flat No, bldgNo=2B, streetNo=Street No, city=City, pincode=123456]
Company Object :::::Company [companyCEO=VGautam, companyAddress=Address [flatNo=Flat No, bldgNo=2B, streetNo=Street No, city=City, pincode=123456]]